15-基础-路由-vue-router-to 属性赋值

<!DOCTYPE html>
<html>

<head>
    <meta charset='UTF-8'>
    <meta name='viewport' content='width=device-width,initial-scale=1.0'>
    <meta http-equiv='X-UA-Compatible' content='ie=edge'>
    <title>Document</title>
</head>

<body>
    <div id='app'>
        <!-- 1.设置链接 -->
        <!-- to值 字符串 -->
        <router-link to="/home">主页</router-link>
        <!-- to值 变量 -->
        <router-link :to="top">热点</router-link>
        <!-- to值 可以是{path等} -->
        <router-link :to="{path:'/aboutus'}">关于我们</router-link>
        <!-- to值 可以是{name等} -->
        <router-link :to="{name:'aaa'}">AAA</router-link>
        <!-- to值 可以是{params:{参数名:值}} -->
        <!-- <router-link :to="{names:'bbb',params:{id:300}}">BBB</router-link> -->
        <router-link :to="{name:'bbb',params:{id:300}}">BBB</router-link>

        <!-- 2. 提供容器:将来渲染组件 -->
        <router-view></router-view>

    </div>
    <script src='./vue.js'></script>
    <script src="./vue-router.js"></script>
    <script>
        // 3. 提供要渲染的组件选项(对象)
        const Home = { template: `<div>首页组件</div>` };
        const Top = { template: `<div>热点组件</div>` };
        const Aboutus = { template: `<div>关于我们组件</div>` };
        const AAA = { template: `<div>AAA组件</div>` };
        // 显示当前的动态路由参数id的值
        const BBB = { template: `<div>BBB组件---{{$route.params.id}}</div>` };

        const routes = [
            { name: "home", path: '/', redirect: { name: 'abc' }, },
            { name: "abc", path: "/top", component: Home },
            { path: "/home", component: Home },
            { path: "/top", component: Top },
            { path: "/aboutus", component: Aboutus },
            { name: "aaa", path: "/aaa", component: AAA },
            { name: "bbb", path: "/bbb/:id", component: BBB }];
        // : to = "{name:'bbb',params:{userId:300}}"
        // 匹配的同时进行传值 传递的是userId的值是300

        // 4. 实例化路由对象
        const router = new VueRouter({ routes })
        // 路由选项 routes

        // 5. 配置路由:匹配当前的标识 渲染对应的组件
        // routes:[{}]
        // routes:routes
        new Vue({
            el: '#app',
            // 6. 挂载(使用)路由
            router,  // router:router
            data: { top: "/top" },
            methods: {}
        });
    </script>
</body>

</html>

posted @ 2019-05-28 23:02  193557749  阅读(3167)  评论(0编辑  收藏  举报