vue-vuerouter重定向中路由重定向星号什么意思

vue router学习之动态路由和嵌套路由详解
作者:0day__
字体:[ ] 类型:转载 时间:
本篇文章主要介绍了vue router 动态路由和嵌套路由,详细的介绍了动态路由和嵌套路由的使用方法,有兴趣的可以了解一下
本文主要参考:
本文的阅读前提是已经能够搭建一个vue前台程序并且运行。如果还么有搭建可以参考文章:
好,下面上货。
首先介绍一下动态路由。
动态路由按照我的理解,就是说能够进行页面的跳转,比如说:下面的这个页面中:
&template&
&div id="app"&
&router-link to="/"&/&/router-link&
&router-link to="/hello"&/hello&/router-link&
&router-link to="/cc"&/cc&/router-link&
&router-view style="border: 1px solid red"&&/router-view&
&/template&
如果点击了/hello,那么在router-view中就会加载对应的模块,也就是在路由中设置的模块。
import Vue from 'vue'
import Router from 'vue-router'
import Hello from '@/components/Hello'
import Foo from '@/components/Foo'
import Foo2 from '@/components/Foo2'
import Foo3 from '@/components/Foo3'
Vue.use(Router)
export default new Router({
{path: '/', redirect: '/hello'},
path: '/hello',
component: Hello,
children: [
{path: '/hello/foo', component: Foo},
{path: '/hello/foo2', component: Foo2},
{path: '/hello/foo3', component: Foo3}
path: '/cc',
name: 'Foo',
component: Foo
也就是说,会跳转到Hello和Foo这两个组件。
那么嵌套路由是什么意思呢,最开始我以为的是这样:/hello/foo 和/hello/foo2这两个路由可以简写成嵌套路由,其实不是的。嵌套路由只的是,在子组件中再次嵌套组件。然后在使用路由进行跳转,这样跳转的时候,变化的就只有子组件,而外边的父组件没有变化。
下面我把完整的例子放出来,看一下:
&template&
&div id="app"&
&router-link to="/"&/&/router-link&
&router-link to="/hello"&/hello&/router-link&
&router-link to="/cc"&/cc&/router-link&
&router-view style="border: 1px solid red"&&/router-view&
&/template&
export default {
name: 'app'
font-family: 'Avenir', Helvetica, Arial, sans-
-webkit-font-smoothing:
-moz-osx-font-smoothing:
text-align:
color: #2c3e50;
margin-top: 60
&template&
&/template&
export default {
name: 'Foo',
&!-- Add "scoped" attribute to limit CSS to this component only --&
&style scoped&
font-weight:
list-style-type:
padding: 0;
display: inline-
margin: 0 10
color: #42b983;
&template&
&h1&this is Foo2&/h1&
&/template&
export default {
name: 'Foo2',
&!-- Add "scoped" attribute to limit CSS to this component only --&
&style scoped&
font-weight:
list-style-type:
padding: 0;
display: inline-
margin: 0 10
color: #42b983;
&template&
&h1&this is foo3&/h1&
&/template&
export default {
name: 'Foo3',
&!-- Add "scoped" attribute to limit CSS to this component only --&
&style scoped&
font-weight:
list-style-type:
padding: 0;
display: inline-
margin: 0 10
color: #42b983;
&template&
&div class="hello"&
&h1&{{ msg }}&/h1&
&h2&Essential Links&/h2&
&li&&a href="https://vuejs.org" rel="external nofollow" target="_blank"&Core Docs&/a&&/li&
&li&&a href="https://forum.vuejs.org" rel="external nofollow" target="_blank"&Forum&/a&&/li&
&li&&a href="https://gitter.im/vuejs/vue" rel="external nofollow" target="_blank"&Gitter Chat&/a&&/li&
&li&&a href="/vuejs" rel="external nofollow" target="_blank"&Twitter&/a&&/li&
&li&&a href="http://vuejs-templates.github.io/webpack/" rel="external nofollow" target="_blank"&Docs for This Template&/a&&/li&
&h2&Ecosystem&/h2&
&li&&a href="http://router.vuejs.org/" rel="external nofollow" target="_blank"&vue-router&/a&&/li&
&li&&a href="http://vuex.vuejs.org/" rel="external nofollow" target="_blank"&vuex&/a&&/li&
&li&&a href="http://vue-loader.vuejs.org/" rel="external nofollow" target="_blank"&vue-loader&/a&&/li&
&li&&a href="/vuejs/awesome-vue" rel="external nofollow" target="_blank"&awesome-vue&/a&&/li&
&router-link to="/hello/foo"&/hello/foo&/router-link&
&router-link to="/hello/foo2"&/hello/foo2&/router-link&
&router-link to="/hello/foo3"&/hello/foo3&/router-link&
&router-view style="border: solid 1px blue"&&/router-view&
&/template&
export default {
name: 'hello',
msg: 'Welcome to Your Vue.js App'
&!-- Add "scoped" attribute to limit CSS to this component only --&
&style scoped&
font-weight:
list-style-type:
padding: 0;
display: inline-
margin: 0 10
color: #42b983;
import Vue from 'vue'
import Router from 'vue-router'
import Hello from '@/components/Hello'
import Foo from '@/components/Foo'
import Foo2 from '@/components/Foo2'
import Foo3 from '@/components/Foo3'
Vue.use(Router)
export default new Router({
{path: '/', redirect: '/hello'},
path: '/hello',
component: Hello,
children: [
{path: '/hello/foo', component: Foo},
{path: '/hello/foo2', component: Foo2},
{path: '/hello/foo3', component: Foo3}
path: '/cc',
name: 'Foo',
component: Foo
需要注意的是仔细的看App.vue和Hello.vue中,都包含&router-view&&/router-view&,但是他们的作用不同,App.vue是顶层路由,指的是组外层的路由,Hello.vue中的是嵌套路由,负责显示子组件。
我把页面截图一下:
这个界面,点击最上边的 / 或者/hello 或者/cc的时候,发生变化的是红色路由中的内容。当点击/hello/foo /hello/foo2 /hello/foo3 的时候,发生变化的是下面蓝色路由中的内容。
这样就和我们平时应用十分的相似了。最外层于有变化,或者局部有变化,但是不想全局的发生改变。
同时,这样也符合了模块化,各个模块分别在不同的模块中。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
您可能感兴趣的文章:
大家感兴趣的内容
12345678910
最近更新的内容
常用在线小工具Posts - 17,
Articles - 1,
Comments - 2
09:26 by WEB前端小菜鸟, ... 阅读,
import Vue from 'vue'import VueRouter from 'vue-router'import App from './App'Vue.use(VueRouter)const router = new VueRouter({
{ path: '/', redirect: '/index' }, //重定向
{ path: '/index', component: resolve =& require(['./components/index.vue'], resolve),
children:[
{ path: 'info', component: resolve =& require(['./components/info.vue'], resolve) }& &//嵌套路由
{ path: '/hello', component: resolve =& require(['./components/hello.vue'], resolve) },//require这种就是按需加载(懒加载)
]})const app = new Vue({
render: h =& h(App)问题对人有帮助,内容完整,我也想知道答案
问题没有实际价值,缺少关键内容,没有改进余地
例如:当前所在url是
/foo/bar?params=123点击一个按钮,要跳转到 this.$router.push('/detail');
希望跳转过去时url是 /detail?params=123
而不是 /detail
查询了文档思路应该是 beforeEach 的时候,调用next时传递一些参数即可,不过大概看了一下文档没理解该怎么用
以下用法导致了路由不断的重定向,应该是next重定向后,又重新进入了beforeEach的钩子里面,然后就死循环了
router.beforeEach((to, from, next) =& {
// to 和 from 都是 路由信息对象
console.log('router msg:');
console.log(to,from,next);
path: to.path,
query: from.query
希望vue老司机指点迷津
答案对人有帮助,有参考价值
答案没帮助,是错误的答案,答非所问
问题已经解决:为啥加了这段就没事了呢
官网上面是这样的,好郁闷~刚刚重新查下了官网~~
分享到微博?
关闭理由:
删除理由:
忽略理由:
推广(招聘、广告、SEO 等)方面的内容
与已有问题重复(请编辑该提问指向已有相同问题)
答非所问,不符合答题要求
宜作评论而非答案
带有人身攻击、辱骂、仇恨等违反条款的内容
无法获得确切结果的问题
非开发直接相关的问题
非技术提问的讨论型问题
其他原因(请补充说明)
我要该,理由是:
在 SegmentFault,解决技术问题
每个月,我们帮助 1000 万的开发者解决各种各样的技术问题。并助力他们在技术能力、职业生涯、影响力上获得提升。
一线的工程师、著名开源项目的作者们,都在这里:
获取验证码
已有账号?}

我要回帖

更多关于 vue router路由重定向 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信