vue 创建路由

缺乏、安全感 2021-07-25 01:22 485阅读 0赞
  1. 路由
  2. 1、安装
  3. cnpm install vue-router -S
  4. 2、在创建路由的js中引入
  5. import Vue from 'vue'
  6. import VueRouter from 'vue-router'
  7. Vue.use(VueRouter)
  8. 3、创建路由
  9. 需要创建new 路由实例对象
  10. const router =/export default new VueRouter({
  11. routes:[
  12. {
  13. path:'',
  14. 可使用*充当占位符
  15. '/user-*',会匹配以/user-开头的任意路径
  16. this.$route.params.pathMatch,会返回*匹配的内容
  17. name:"", 路由名称,当跳转时指定name属性使用
  18. component: 使用前先引入组件
  19. meta:{键值对} 给路由添加元信息,可通过this/$route.meta.键名获取
  20. }
  21. ]
  22. })
  23. 4、在主入口组件中(App.vue)使用
  24. <router-view />
  25. 5、在main.js
  26. (1)引入路由文件
  27. import router from './router'
  28. (2)在vue的实例化对象中添加route
  29. new Vue({
  30. el: '#app',
  31. router,
  32. components: { App },
  33. template: '<App/>'
  34. })

代码示例:
main.js:

  1. // The Vue build version to load with the `import` command
  2. // (runtime-only or standalone) has been set in webpack.base.conf with an alias.
  3. import Vue from 'vue'
  4. import App from './App'
  5. import Axios from 'axios'
  6. import VueRouter from 'vue-router'
  7. import HelloWorld from './components/HelloWorld'
  8. Vue.use(VueRouter)
  9. Vue.config.productionTip = false
  10. const router=new VueRouter({
  11. routes:[
  12. {
  13. path:'/hello',
  14. name:"HelloWorld",
  15. component:HelloWorld
  16. }
  17. ]
  18. })
  19. /* eslint-disable no-new */
  20. new Vue({
  21. el: '#app',
  22. router,
  23. components: { App },
  24. template: '<App/>'
  25. })

App.vue:

  1. <template>
  2. <div id="app">
  3. <img src="./assets/logo.png">
  4. <router-view />
  5. </div>
  6. </template>
  7. <script>
  8. export default {
  9. name: 'App',
  10. data()
  11. {
  12. return{
  13. }
  14. }
  15. </script>
  16. <style>
  17. #app {
  18. font-family: 'Avenir', Helvetica, Arial, sans-serif;
  19. -webkit-font-smoothing: antialiased;
  20. -moz-osx-font-smoothing: grayscale;
  21. text-align: center;
  22. color: #2c3e50;
  23. margin-top: 60px;
  24. }
  25. </style>

发表评论

表情:
评论列表 (有 0 条评论,485人围观)

还没有评论,来说两句吧...

相关阅读

    相关 vue

    一、App.vue中的<router-view></router-view> 凡是定义到index.js中的组件,都会被渲染到App.vue中 二、路由跳转和a标签的

    相关 vue

    Vue.js - vue路由 一 什么是路由 1. 后端路由:对于普通的网站,所有的超链接都是URL地址,所有的URL地址都对应服务器上对应的资源; 2. 前端