Vue第十一天(路由)

准备步骤如同第一天

代码:(测试注意事项:js中的代码依次开启测试,不可全部开启,会报错)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="../node_modules/vue/dist/vue.js"></script>
    <script src="../node_modules/vue-resource/dist/vue-resource.js"></script>
    <script src="../node_modules/vue-router/dist/vue-router.js"></script>
</head>
<body>
<!--有点深奥,没弄懂,需要认真研究-->
<div id="app1">
    <h1>测试Vue路由</h1>
    <p>
        <!--使用router-link组件导航-->
        <!--通过传入’to‘属性指令链接-->
        <!--<fouter-link>默认会被渲染成一个’<a>‘标签-->
        <router-link to="/foo">测试Foo</router-link>
        <router-link to="/Bar">测试Bar</router-link>
    </p>
    <!--路由出口-->
    <!--路由匹配到的组件将渲染在这里-->
    <router-view></router-view>
</div>
</body>
<script>
    // 0. 如果使用模块化机制编程,導入Vue和VueRouter,要调用 Vue.use(VueRouter)

    // 1. 定义(路由)组件。
    // 可以从其他文件 import 进来
    const Foo = { template: '<div>foo</div>' }
    const Bar = { template: '<div>bar</div>' }

    // 2. 定义路由
    // 每个路由应该映射一个组件。 其中"component" 可以是通过 Vue.extend() 创建的组件构造器,
    // 或者,只是一个组件配置对象。
    // 我们晚点再讨论嵌套路由。
    const routes = [
        { path: '/foo', component: Foo },
        { path: '/bar', component: Bar }
    ]

    // 3. 创建 router 实例,然后传 `routes` 配置
    // 你还可以传别的配置参数, 不过先这么简单着吧。
    const router = new VueRouter({
        routes // (缩写)相当于 routes: routes
    })

    // 4. 创建和挂载根实例。
    // 记得要通过 router 配置参数注入路由,
    // 从而让整个应用都有路由功能
    const app = new Vue({
        router
    }).$mount('#app1')

</script>
</html>

猜你喜欢

转载自blog.csdn.net/qq_31051117/article/details/84028073