路由允许通过不同URL访问不同内容 可以实现多视图的单页Web应用


const声明一个常量

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8" />
  5. <title></title>
  6. </head>
  7. <body>
  8. <script src="https://cdn.staticfile.org/vue/2.4.2/vue.min.js"></script>
  9. <script type="text/jscript">
  10. ///const 用法
  11. const PI =3.14;
  12. // PI=123; // 报错ncaught TypeError: Assignment to constant variable
  13. const Person = {
  14. name : '张三',
  15. age : '30'
  16. }
  17. console.log(Person.name, Person.age)
  18. Person.name = '李四'
  19. // 注意 const 声明的对象的属性是可以修改的
  20. Person.age = '40'
  21. console.log(Person.name, Person.age)
  22. </script>
  23. </body>
  24. </html>

Vue Router

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8" />
  5. <title>Vue路由</title>
  6. <script src="https://cdn.staticfile.org/vue/2.4.2/vue.min.js"></script>
  7. <script src="https://cdn.staticfile.org/vue-router/2.7.0/vue-router.min.js"></script>
  8. </head>
  9. <body>
  10. <div id="app">
  11. <h1>Hello App!</h1>
  12. <p>
  13. <!-- 使用 router-link 组件来导航. -->
  14. <!-- 通过传入 `to` 属性指定链接. -->
  15. <!-- <router-link> 默认会被渲染成一个 `<a>` 标签 -->
  16. <router-link to="/foo">Go to Foo</router-link>
  17. <router-link to="/bar">Go to Bar</router-link>
  18. </p>
  19. <!-- 路由出口 -->
  20. <!-- 路由匹配到的组件将渲染在这里 -->
  21. <router-view></router-view>
  22. </div>
  23. <script>
  24. // 0. 如果使用模块化机制编程,導入Vue和VueRouter,要调用 Vue.use(VueRouter)
  25. // 1. 定义(路由)组件。
  26. // 可以从其他文件 import 进来
  27. const Foo = { template: "<div>foo</div>" };
  28. const Bar = { template: "<div>bar</div>" };
  29. // 2. 定义路由
  30. // 每个路由应该映射一个组件。 其中"component" 可以是
  31. // 通过 Vue.extend() 创建的组件构造器,
  32. // 或者,只是一个组件配置对象。
  33. // 我们晚点再讨论嵌套路由。
  34. const routes = [
  35. { path: "/foo", component: Foo },
  36. { path: "/bar", component: Bar },
  37. ];
  38. // 3. 创建 router 实例,然后传 `routes` 配置
  39. // 你还可以传别的配置参数, 不过先这么简单着吧。
  40. const router = new VueRouter({
  41. routes, // (缩写)相当于 routes: routes
  42. });
  43. // 4. 创建和挂载根实例。
  44. // 记得要通过 router 配置参数注入路由,
  45. // 从而让整个应用都有路由功能
  46. const app = new Vue({
  47. router,
  48. }).$mount("#app");
  49. // 现在,应用已经启动了!
  50. </script>
  51. </body>
  52. </html>

点击过的导航链接会加上class =”router-link-exact-active router-link-active”样式