vue路由--命名路由

 

有时我们通过一个名称来标识一个路由显得更方便一些,特别是在链接一个路由,或者是执行一些跳转的时候。你可以在创建 Router 实例的时候,在 routes 配置中给某个路由设置名称。

我们直接在路由下添加一个 name 即可.

 注意,如果使用命名路由route-link的to属性前面有有个冒号(对比标红部分)
<!DOCTYPE html>
<!-- saved from url=(0077)https://keepfool.github.io/vue-tutorials/06.Router/basic/basic_01.html#!/home -->
<html>
 
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>abc</title>
    <link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css">
    <link rel="stylesheet" href="./basic_01_files/custom.css">
</head>
 
<body>
    <div id="app">
        <div class="row">
            <div class="col-xs-offset-2 col-xs-8">
                <div class="page-header">
                    <h2>Router Basic - 01</h2>
                </div>
            </div>
        </div>
        <div class="row">
            <div class="col-xs-2 col-xs-offset-2">
                <div class="list-group">
                     
                    <router-link class="list-group-item" :to="{ name: 'home'}">Go to Foo</router-link>
                    <router-link class="list-group-item" to="/about">Go to Bar</router-link>
                </div>
            </div>
            <router-view></router-view>
        </div>
    </div>
    <template id="home">
        <div>
            <h1>Home</h1>
            <p>{{msg}}</p>
        </div>
    </template>
    
    <script src="https://cdn.bootcss.com/vue/2.4.2/vue.min.js"></script>
    <script src="https://cdn.bootcss.com/vue-router/2.7.0/vue-router.min.js"></script>
    <script>
     
    var todoItem = Vue.extend({
        data: function() {
            return {
                todoData: [
                    { id: 0, text: '蔬菜' },
                    { id: 1, text: '奶酪' },
                    { id: 2, text: '随便其它什么人吃的东西' }
                ]
            };
        },
        template: `
        <ul>
            <li v-for='(d, i) in todoData' :key="i">
                {{ d.text }}
            </li>
        </ul>
  `,
 
    });
 
    var t_test = Vue.extend({
        data:function(){
            return {
                msg:"hello,test"
            };
        },
        template:"#home"
 
        }
 
    );
 
 
 
    // Home = { template: '<div>foo</div>' }
    // About = { template: '<div>bar</div>' }
 
    routes = [
        { path: '/home',name:"home",component: t_test },
        { path: '/about', component: todoItem }
    ]
 
    router = new VueRouter({
        routes: routes // (缩写)相当于 routes: routes
    });
 
    app = new Vue({
        router: router
    }).$mount('#app');
    </script>
</body>
 
</html>

 

命名动态路由:

<!DOCTYPE html>
<html lang="en">
 
<head>
    <meta charset="UTF-8">
    <title>abc</title>
    <script src="https://unpkg.com/vue/dist/vue.js"></script>
    <script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
</head>
 
<body>
    <script src="https://unpkg.com/vue/dist/vue.js"></script>
    <script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
    <div id="app">
        <h1>Hello App!</h1>
        <p>
            <!-- 使用 router-link 组件来导航. -->
            <!-- 通过传入 `to` 属性指定链接. -->
            <!-- <router-link> 默认会被渲染成一个 `<a>` 标签 -->
            <router-link :to="{ name: 'id',params:{id:1}}">foo1</router-link>
            <router-link to="/foo/2">foo2</router-link>
        </p>
        <!-- 路由出口 -->
        <!-- 路由匹配到的组件将渲染在这里 -->
        <router-view></router-view>
    </div>
</body>
<script type="text/javascript">
// 0. 如果使用模块化机制编程,导入 Vue 和 VueRouter,要调用 Vue.use(VueRouter)
 
// 1. 定义(路由)组件。
// 可以从其他文件 import 进来
const Foo = { template: '<div>foo:{{ $route.params.id }}</div>' }
 
// 2. 定义路由
// 每个路由应该映射一个组件。 其中"component" 可以是
// 通过 Vue.extend() 创建的组件构造器,
// 或者,只是一个组件配置对象。
// 我们晚点再讨论嵌套路由。
const routes = [
    { path: '/foo/:id', name:"id",component: Foo }
]
 
// 3. 创建 router 实例,然后传 `routes` 配置
// 你还可以传别的配置参数, 不过先这么简单着吧。
const router = new VueRouter({
    routes // (缩写)相当于 routes: routes
})
 
// 4. 创建和挂载根实例。
// 记得要通过 router 配置参数注入路由,
// 从而让整个应用都有路由功能
const app = new Vue({
    router // (缩写)相当于 router: router
 
}).$mount('#app')
</script>
 
</html>

 多参数:

<!DOCTYPE html>
<html lang="en">
 
<head>
    <meta charset="UTF-8">
    <title>abc</title>
    <script src="https://unpkg.com/vue/dist/vue.js"></script>
    <script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
</head>
 
<body>
    <script src="https://unpkg.com/vue/dist/vue.js"></script>
    <script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
    <div id="app">
        <h1>Hello App!</h1>
        <p>
            <!-- 使用 router-link 组件来导航. -->
            <!-- 通过传入 `to` 属性指定链接. -->
            <!-- <router-link> 默认会被渲染成一个 `<a>` 标签 -->
            <router-link :to="{name:'ids',params:{id1:1,id2:11}}">foo1</router-link>
            <router-link to="/foo/2/22">foo2</router-link>
        </p>
        <!-- 路由出口 -->
        <!-- 路由匹配到的组件将渲染在这里 -->
        <router-view></router-view>
    </div>
</body>
<script type="text/javascript">
// 0. 如果使用模块化机制编程,导入 Vue 和 VueRouter,要调用 Vue.use(VueRouter)
 
// 1. 定义(路由)组件。
// 可以从其他文件 import 进来
// const Foo = { template: '<div>foo:{{ $route.params.id }}</div>' }
const Foo = Vue.extend({
 
    template: '<div id="testzy"></div>',
    methods: {
        change() {
          //alert("come");
          document.getElementById("testzy").innerText = this.$route.params.id1+","+this.$route.params.id2;
            //alert("fd");
            // if (this.$route.params.id == 1) {
            //     document.getElementById("testzy").innerText = "hehe";
            // } else {
            //     document.getElementById("testzy").innerText = "haha";
            // }
        },
    },
    mounted(){
      document.getElementById("testzy").innerText = this.$route.params.id1+","+this.$route.params.id2;
    },
    watch:{$route:"change"}
});
 
// 2. 定义路由
// 每个路由应该映射一个组件。 其中"component" 可以是
// 通过 Vue.extend() 创建的组件构造器,
// 或者,只是一个组件配置对象。
// 我们晚点再讨论嵌套路由。
const routes = [
    { path: '/foo/:id1/:id2',name:"ids" ,component: Foo }
]
 
// 3. 创建 router 实例,然后传 `routes` 配置
// 你还可以传别的配置参数, 不过先这么简单着吧。
const router = new VueRouter({
    routes // (缩写)相当于 routes: routes
})
 
 
 
// 4. 创建和挂载根实例。
// 记得要通过 router 配置参数注入路由,
// 从而让整个应用都有路由功能
const app = new Vue({
    router, // (缩写)相当于 router: router
 
}).$mount('#app')
</script>
 
</html>

 

动态+命名+push:

<!DOCTYPE html>
<html lang="en">
 
<head>
    <meta charset="UTF-8">
    <title>abc</title>
    <script src="https://unpkg.com/vue/dist/vue.js"></script>
    <script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
</head>
 
<body>
    <div id="app">
        <h1>Hello App!</h1>
        <!-- 路由出口 -->
        <!-- 路由匹配到的组件将渲染在这里 -->
        <router-view>
        </router-view>
    </div>
</body>
<script type="text/javascript">

 
const test = Vue.extend({
    template: '<a @click="pushtest" href="javascript:void(0)">Navigate to {{$route.params.name}}</a>',
    methods: {
        pushtest() {
            //alert("bar");
            if (this.$route.params.name == "bar") {
                this.$router.push({ name: 'test',params:{name:"foo"} });
            }else{
                this.$router.push({ name: 'test',params:{name:"bar"} });
            }
            
            //alert("fdas");
        },
    },
 
});
 

// 2. 定义路由
// 每个路由应该映射一个组件。 其中"component" 可以是
// 通过 Vue.extend() 创建的组件构造器,
// 或者,只是一个组件配置对象。
// 我们晚点再讨论嵌套路由。
const routes = [
    { path: '/', redirect: "/test/bar" },
    // { path: '/foo', name: "foo", component: Foo },
    { path: '/test/:name', name: "test", component: test },
 
]
 
// 3. 创建 router 实例,然后传 `routes` 配置
// 你还可以传别的配置参数, 不过先这么简单着吧。
const router = new VueRouter({
    routes // (缩写)相当于 routes: routes
})
 
// 4. 创建和挂载根实例。
// 记得要通过 router 配置参数注入路由,
// 从而让整个应用都有路由功能
const app = new Vue({
    router, // (缩写)相当于 router: router
 
}).$mount('#app') // 现在,应用已经启动了!
</script>
 
</html>

如果要带query参数,可以这样:

<!DOCTYPE html>
<html lang="en">
 
<head>
    <meta charset="UTF-8">
    <title>abc</title>
    <script src="https://unpkg.com/vue/dist/vue.js"></script>
    <script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
</head>
 
<body>
    <div id="app">
        <h1>Hello App!</h1>
        <!-- 路由出口 -->
        <!-- 路由匹配到的组件将渲染在这里 -->
        <router-view>
        </router-view>
    </div>
</body>
<script type="text/javascript">

 
const test = Vue.extend({
    template: '<a @click="pushtest" href="javascript:void(0)">Navigate to {{$route.params.name}}</a>',
    methods: {
        pushtest() {
            //alert("bar");
            if (this.$route.params.name == "bar") {
                this.$router.push({ name: 'test',params:{name:"foo"} ,query:{ param: 'foo' }});
            }else{
                this.$router.push({ name: 'test',params:{name:"bar"} ,query:{ param: 'bar' }});
            }
            
            //alert("fdas");
        },
    },
 
});
 

// 2. 定义路由
// 每个路由应该映射一个组件。 其中"component" 可以是
// 通过 Vue.extend() 创建的组件构造器,
// 或者,只是一个组件配置对象。
// 我们晚点再讨论嵌套路由。
const routes = [
    { path: '/', redirect: "/test/bar" },
    // { path: '/foo', name: "foo", component: Foo },
    { path: '/test/:name', name: "test", component: test },
 
]
 
// 3. 创建 router 实例,然后传 `routes` 配置
// 你还可以传别的配置参数, 不过先这么简单着吧。
const router = new VueRouter({
    routes // (缩写)相当于 routes: routes
})
 
// 4. 创建和挂载根实例。
// 记得要通过 router 配置参数注入路由,
// 从而让整个应用都有路由功能
const app = new Vue({
    router, // (缩写)相当于 router: router
 
}).$mount('#app') // 现在,应用已经启动了!
</script>
 
</html>

 网址是类似这样的:

xxxxx.html#/test/foo?param=foo

 

转载于:https://www.cnblogs.com/cowboybusy/p/9517393.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue Router 是 Vue.js 官方的路由管理器。它和 Vue.js 的核心深度集成,可以非常方便地在单页应用中管理应用的路由。 在使用 Vue Router 进行路由管理时,我们需要用到它的一个重要组件 router-view。router-view 可以理解为路由的容器,用于显示当前路由对应的组件。 下面我们来详细介绍一下 router-view 的使用方式和相关属性。 ### 基本用法 在 Vue Router 中使用 router-view 很简单,只需要在需要展示路由组件的地方加上 <router-view></router-view> 标签即可。 例如,我们有一个路由配置如下: ```js const routes = [ { path: '/', name: 'home', component: Home }, { path: '/about', name: 'about', component: About } ] ``` 在需要展示路由组件的地方添加 <router-view></router-view> 标签: ```html <template> <div> <router-view></router-view> </div> </template> ``` 这样,当路由为 '/' 时,会显示 Home 组件;当路由为 '/about' 时,会显示 About 组件。 ### 嵌套路由Vue Router 中,我们可以使用嵌套路由来实现复杂的路由配置。当我们需要在一个父路由下展示多个子路由时,可以使用嵌套路由。 例如,我们有一个父路由 '/user',下面有两个子路由 '/user/profile' 和 '/user/settings',路由配置如下: ```js const routes = [ { path: '/user', component: User, children: [ { path: 'profile', component: Profile }, { path: 'settings', component: Settings } ] } ] ``` 在 User 组件中,我们需要使用 router-view 来展示子路由对应的组件: ```html <template> <div> <h1>User Page</h1> <router-view></router-view> </div> </template> ``` 这样,当路由为 '/user/profile' 时,会显示 Profile 组件;当路由为 '/user/settings' 时,会显示 Settings 组件。 ### 路由命名视图 在某些情况下,我们需要在同一个页面中展示多个路由组件。此时,我们可以使用路由命名视图来实现。 路由命名视图是一种特殊的 router-view,它可以指定路由组件的渲染位置。 例如,我们有一个路由配置如下: ```js const routes = [ { path: '/', components: { default: Home, header: Header, footer: Footer } } ] ``` 这里我们通过 components 属性来指定每个路由命名视图对应的组件。default 表示默认的视图,也就是没有指定名字的视图。 在需要展示路由组件的地方,我们需要使用 router-view 的 name 属性来指定命名视图的名称: ```html <template> <div> <router-view name="header"></router-view> <router-view></router-view> <router-view name="footer"></router-view> </div> </template> ``` 这样,我们就可以在同一个页面中展示多个路由组件了。 ### 路由过渡动画 在 Vue Router 中,可以通过 transition 组件来给路由切换添加过渡动画。 首先,我们需要在 App.vue 中添加 transition 组件: ```html <template> <div> <transition name="fade"> <router-view></router-view> </transition> </div> </template> ``` 这里我们使用 name 属性来指定过渡动画的名称,fade 是一个自定义的名称。 接下来,我们需要在 CSS 中定义过渡动画: ```css .fade-enter-active, .fade-leave-active { transition: opacity .5s; } .fade-enter, .fade-leave-to { opacity: 0; } ``` 这里我们使用 opacity 属性来控制元素的透明度,当元素进入或离开时,会触发过渡动画。 至此,我们已经介绍了 router-view 的基本用法、嵌套路由路由命名视图和路由过渡动画。希望对你有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值