Vue中的路由 以及默认路由跳转

https://router.vuejs.org/
vue路由配置:
    1.安装 
    npm install vue-router  --save   / cnpm install vue-router  --save
    2、引入并 Vue.use(VueRouter)   (main.js)
        import VueRouter from 'vue-router'
        Vue.use(VueRouter)
    3、配置路由
        1、创建组件 引入组件
        2、定义路由  (建议复制s)
            const routes = [
              { path: '/foo', component: Foo },
              { path: '/bar', component: Bar },
              { path: '*', redirect: '/home' }   /*默认跳转路由*/
            ]
        3、实例化VueRouter
            const router = new VueRouter({
              routes // (缩写)相当于 routes: routes
            })

        4、挂载    
        new Vue({
          el: '#app',
          router,
          render: h => h(App)
        })
        5 、根组件的模板里面放上这句话   <router-view></router-view>         
        6、路由跳转
        <router-link to="/foo">Go to Foo</router-link>
         <router-link to="/bar">Go to Bar</router-link>

main.js

import Vue from 'vue';
import App from './App.vue';



import VueRouter from 'vue-router';

Vue.use(VueRouter);


//1.创建组件


import Home from './components/Home.vue';

import News from './components/News.vue';


//2.配置路由   注意:名字

const routes = [
  { path: '/home', component: Home },
  { path: '/news', component: News },

  { path: '*', redirect: '/home' }   /*默认跳转路由*/
]


//3.实例化VueRouter  注意:名字

const router = new VueRouter({
  routes // (缩写)相当于 routes: routes
})




//4、挂载路由

new Vue({
  el: '#app',
  router,
  render: h => h(App)
})


//5 <router-view></router-view> 放在 App.vue
<template>


  <div id="app"> 


    <router-link to="/home">首页</router-link>
    <router-link to="/news">新闻</router-link>

    <hr>

       <router-view></router-view>

  </div>
</template>

<script>

   export default {     
      data () { 
        return {
         
         msg:'你好vue'
        }
      }
     
    }
</script>
<style lang="scss">


</style>
<template>
    <!-- 所有的内容要被根节点包含起来 -->
    <div id="home">    
       我是首页组件
    </div>
</template>


<script>
    export default{
        data(){
            return {               
               msg:'我是一个home组件'
            }
        }
    }

</script>

<style lang="scss" scoped>
    
</style>
<template>    
    <div id="news">    
       我是新闻组件   


       
          
    </div>

</template>


<script>

    export default{
        data(){
            return {               
               msg:'我是一个新闻组件'              
            }
        }
    }

</script>

<style lang="scss" scoped>
    
</style>

 

上一篇:php – Laravel:将任意URL解析为相应的Controller / Route?


下一篇:php – Laravel NotFoundHttpException