Vue路由注意事项

隐身守侯 提交于 2019-11-30 09:46:37

一、vue中路由的使用

1、定义组件

<template>
  <div class="hello">
    <h1 @click="info" :class="color">{{ msg }}</h1>
  </div>
</template>

<script>
export default {
  name: 'hello',
  data () {
    return {
      msg: '我是Hello组件',
      color:'color'
    }
  },
  methods:{
      info(){
          console.log('你点击了我');
      }
  }
}
</script>

<style>
    .color{
        color:#630;
    }
</style>

2、配置路由文件

import Vue from 'vue'
import Router from 'vue-router'
import Hello from '@/components/Hello'
import Word from '@/components/Word';
Vue.use(Router)

export default new Router({
    routes: [
        {
            path: '/',
            component: Hello
        },
        {
            path:'/index',
            component:Word
        }
    ]
})

3、配置路由插座<router-view></router-view>

<template>
  <div id="app">
    <!--可以定义不变的内容-->
    <h3>{{title}}</h3>
    <!--定义路由插座-->
    <router-view></router-view>
    <!--可以定义不变的内容-->
  </div>
</template>

<script>
export default{
    name:'app',
    data(){
        return{
            title:'我是项目主入口'
        }
    }
}
</script>

4、路由文件注入到main.js文件中

import Vue from 'vue';
import Router from 'vue-router';
import App from './App';
import router from './router/index';


Vue.config.productionTip = false;
/* eslint-disable no-new */
new Vue({
    el: '#app',
    router,
    render(h){
        return h(App);
    }
})


二、配置路由的跳转

路由的跳转使用标签router-link
1、知道路径的跳转

<ul>
    <li><router-link to="/">Hello页面</router-link></li>
    <li><router-link to="/word">word页面</router-link></li>
</ul>
<!-- 定义路由插座 -->
<router-view></router-view>

2、to是通过绑定数据到上面

<ul>
    <li><router-link to="/">Hello页面</router-link></li>
    <li><router-link :to="word">word页面</router-link></li>
</ul>
<!-- 定义路由插座 -->
<router-view></router-view>
<script>
export default{
    name:'app',
    data(){
        return{
            title:'我是项目主入口',
            word:'/word'
        }
    }
}
</script>


三、定义子路由

1、定义路由跳转

<ul>
    <li><router-link to="/word/router1">路由1</router-link></li>
    <li><router-link to="/word/router2">路由2</router-link></li>
    <router-view></router-view>
</ul>

2、路由的跳转

{
    path:'/word',
    component:Word,
    children:[
        {
            path:'router1',
            component:Router1
        },
        {
            path:'router2',
            component:Router2
        }
    ]
}


四、路由之间传递参数params方式

1、路由跳转的时候定义要传递参数形式

{
    path:'router1/:id',
    component:Router1
},

2、页面跳转的时候传递参数

<!--123就是传递的id值-->
<li><router-link to="/word/router1/123">路由1</router-link></li>

3、在组件中接收传递过去的参数

export default{
    mounted(){
        console.log(this.$route);
        console.log(this.$route.params.id);
    }
}


五、路由之间传递参数query方式

1、在路由跳转地方传递query参数

<li><router-link v-bind:to="{path:'/word/router2',query:{id:123}}">路由2</router-link></li>

2、在组件的mounted中接收

export default{
    mounted(){
        console.log(this.$route);
        console.log(this.$route.query.id);
    }
}


六、关于vue脚本中操作页面的跳转及传递参数

1、使用push实现页面跳转

methods:{
    go1(){
        // 使用params传递参数
        this.$router.push({path:'/word/router1/123'});
    }
}

2、使用replace实现页面的跳转

methods:{
    go2(){
        // 使用query传递参数
        this.$router.replace({path:'/word/router2',query:{id:123}});
    }
}

七、关于前进与后退


1、页面代码

<input type="button" value="前进" @click="next"/>
<input type="button" value="后进" @click="prevent"/>

2、事件方法代码

methods:{
    next(){
        this.$router.go(1);
    },
    prevent(){
        this.$router.go(-1);
    }
}


八、重定向

1、配置路由

{
    path:'router',  // path路径 
    redirect:'/word/router3'  // 重定向指向的路径
}

2、配置页面跳转

<li><router-link to="/word/router">路由4</router-link></li>

3、重定向函数

{
    path:'router5',
    redirect:()=>{
        console.log(arguments);
        return '/word/router3';
    }
}


九、路由的钩子函数

1、beforeEnter的使用

{
    path:'router2',
    component:Router2,
    beforeEnter(to,form,next){
        console.log('///',arguments);
        setTimeout(()=>(next()),1000);
    }
},

2、beforeRouteUpdate的使用

3、beforeRouteLeave的使用


十、路由实现组件间传递数据

1、直接传递到组件

<Myhead v-bind:name1="name1"></Myhead>
1

<script>

    import Myheadfrom '@/components/Myhead';
    export default{
        name:'app',
        data(){
            return{
                name1:'张三'
            }
        },
        components:{
            Myhead
        }
    }
    </script>

2、组件Myhead中接收

export default{
    props:['name1'],
}

3、传递到router-view上

<router-view v-bind:age="age"></router-view>

export default{
    name:'word',
    props:['age'],
}

 

 

 

http://www.beimohome.cn
http://www.wu0553.com/m/view.php?aid=28674
http://www.wu0553.com/m/view.php?aid=28672
http://www.wu0553.com/m/view.php?aid=28670
http://www.wu0553.com/m/view.php?aid=28669
http://www.wu0553.com/m/view.php?aid=28667
http://www.wu0553.com/m/view.php?aid=28664
http://www.wu0553.com/m/view.php?aid=28662
http://www.wu0553.com/m/view.php?aid=28659
http://www.wu0553.com/m/view.php?aid=28658
http://www.wu0553.com/m/view.php?aid=28657
http://www.wu0553.com/m/view.php?aid=28656
http://www.wu0553.com/m/view.php?aid=28655
http://www.wu0553.com/m/view.php?aid=28654
http://www.wu0553.com/m/view.php?aid=28653
http://www.wu0553.com/m/view.php?aid=28652
http://www.wu0553.com/m/view.php?aid=28647
http://www.wu0553.com/m/view.php?aid=28642
http://www.wu0553.com/m/view.php?aid=28637
http://www.wu0553.com/m/view.php?aid=28630
http://www.wu0553.com/m/view.php?aid=28625
http://www.wu0553.com/m/view.php?aid=28620
http://www.wu0553.com/m/view.php?aid=28614
http://www.wu0553.com/m/view.php?aid=28608
http://www.wu0553.com/m/view.php?aid=28601
http://www.wu0553.com/m/view.php?aid=28591
http://www.wu0553.com/m/view.php?aid=28581
http://www.wu0553.com/m/view.php?aid=28570
http://www.wu0553.com/m/view.php?aid=28559
http://www.wu0553.com/m/view.php?aid=28549
http://www.wu0553.com/m/view.php?aid=28539
http://www.wu0553.com/m/view.php?aid=28531
http://www.wu0553.com/m/view.php?aid=28520
http://www.wu0553.com/m/view.php?aid=28511
http://www.wu0553.com/m/view.php?aid=28499
http://www.wu0553.com/m/view.php?aid=28487
http://www.wu0553.com/m/view.php?aid=28476
http://www.wu0553.com/m/view.php?aid=28469
http://www.wu0553.com/m/view.php?aid=28460
http://www.wu0553.com/m/view.php?aid=28450
http://www.wu0553.com/m/view.php?aid=28442
http://www.wu0553.com/m/view.php?aid=28436
http://www.wu0553.com/m/view.php?aid=28431
http://www.wu0553.com/m/view.php?aid=28427
http://www.wu0553.com/m/view.php?aid=28422
http://www.wu0553.com/m/view.php?aid=28419
http://www.wu0553.com/m/view.php?aid=28408
http://www.wu0553.com/m/view.php?aid=28398
http://www.wu0553.com/m/view.php?aid=28385
http://www.wu0553.com/m/view.php?aid=28376
http://www.wu0553.com/m/view.php?aid=28364
http://www.wu0553.com/news/28785.html
http://www.wu0553.com/news/20190924/28784.html
http://www.wu0553.com/news/28782.html
http://www.wu0553.com/news/20190924/28780.html
http://www.wu0553.com/news/28777.html
http://www.wu0553.com/news/28773.html
http://www.wu0553.com/news/28770.html
http://www.wu0553.com/news/28767.html
http://www.wu0553.com/news/28765.html
http://www.wu0553.com/news/28761.html
http://www.wu0553.com/news/28758.html
http://www.wu0553.com/news/28755.html
http://www.wu0553.com/news/28754.html
http://www.wu0553.com/news/28752.html
http://www.wu0553.com/news/28751.html
http://www.wu0553.com/news/28748.html
http://www.wu0553.com/news/20190924/28746.html
http://www.wu0553.com/news/28743.html
http://www.wu0553.com/news/20190924/28741.html
http://www.wu0553.com/news/28739.html
http://www.wu0553.com/news/28738.html
http://www.wu0553.com/news/28735.html
http://www.wu0553.com/news/28733.html
http://www.wu0553.com/news/28732.html
http://www.wu0553.com/news/28729.html
http://www.wu0553.com/news/28726.html
http://www.wu0553.com/news/28723.html
http://www.wu0553.com/news/28718.html
http://www.wu0553.com/news/28716.html
http://www.wu0553.com/news/28715.html
http://www.wu0553.com/news/20190924/28713.html
http://www.wu0553.com/news/28710.html
http://www.wu0553.com/news/20190924/28707.html
http://www.wu0553.com/news/28704.html
http://www.wu0553.com/news/28702.html
http://www.wu0553.com/news/28701.html
http://www.wu0553.com/news/28698.html
http://www.wu0553.com/news/28695.html
http://www.wu0553.com/news/28693.html
http://www.wu0553.com/news/28691.html
http://www.wu0553.com/news/28687.html
http://www.wu0553.com/news/28685.html
http://www.wu0553.com/news/28683.html
http://www.wu0553.com/news/28681.html
http://www.wu0553.com/news/28679.html
http://www.wu0553.com/news/20190924/28676.html
http://www.wu0553.com/news/28674.html
http://www.wu0553.com/news/20190924/28672.html
http://www.wu0553.com/news/28670.html
http://www.wu0553.com/news/28669.html
http://www.wu0553.com/news/28667.html
http://www.wu0553.com/news/28664.html
http://www.wu0553.com/news/28662.html
http://www.wu0553.com/news/28659.html
http://www.wu0553.com/news/28658.html
http://www.wu0553.com/news/28657.html
http://www.wu0553.com/news/28656.html
http://www.wu0553.com/news/28655.html
http://www.wu0553.com/news/28654.html
http://www.wu0553.com/news/28653.html
http://www.wu0553.com/news/20190924/28652.html
http://www.wu0553.com/news/28647.html
http://www.wu0553.com/news/20190924/28642.html
http://www.wu0553.com/news/28637.html
http://www.wu0553.com/news/28630.html
http://www.wu0553.com/news/28625.html
http://www.wu0553.com/news/28620.html
http://www.wu0553.com/news/28614.html
http://www.wu0553.com/news/28608.html
http://www.wu0553.com/news/28601.html
http://www.wu0553.com/news/28591.html
http://www.wu0553.com/news/28581.html
http://www.wu0553.com/news/28570.html
http://www.wu0553.com/news/28559.html
http://www.wu0553.com/news/28549.html
http://www.wu0553.com/news/20190924/28539.html
http://www.wu0553.com/news/28531.html
http://www.wu0553.com/news/20190924/28520.html
http://www.wu0553.com/news/28511.html
http://www.wu0553.com/news/28499.html
http://www.wu0553.com/news/28487.html
http://www.wu0553.com/news/28476.html
http://www.wu0553.com/news/28469.html
http://www.wu0553.com/news/28460.html
http://www.wu0553.com/news/28450.html
http://www.wu0553.com/news/28442.html
http://www.wu0553.com/news/28436.html
http://www.wu0553.com/news/28431.html
http://www.wu0553.com/news/28427.html
http://www.wu0553.com/news/28422.html
http://www.wu0553.com/news/20190924/28419.html
http://www.wu0553.com/news/20190924/28408.html
http://www.wu0553.com/news/28398.html
http://www.wu0553.com/news/28385.html
http://www.wu0553.com/news/28376.html
http://www.wu0553.com/news/28364.html
http://www.wu0553.com/news/28352.html
http://www.wu0553.com/news/28340.html
http://www.wu0553.com/news/28329.html
http://www.wu0553.com/news/28317.html
http://www.wu0553.com/news/28311.html
http://www.wu0553.com/news/28304.html
http://www.wu0553.com/news/28294.html
http://www.wu0553.com/news/28285.html
http://www.wu0553.com/news/20190924/28274.html
http://www.wu0553.com/news/28265.html
http://www.wu0553.com/news/20190924/28253.html
http://www.wu0553.com/news/28246.html
http://www.wu0553.com/news/28234.html
http://www.wu0553.com/news/28229.html
http://www.wu0553.com/news/28224.html
http://www.wu0553.com/news/28214.html
http://www.wu0553.com/news/28204.html
http://www.wu0553.com/news/28196.html
http://www.wu0553.com/news/28187.html
http://www.wu0553.com/news/28179.html
http://www.wu0553.com/news/28175.html
http://www.wu0553.com/news/28172.html
http://www.wu0553.com/news/28167.html
http://www.wu0553.com/m/view.php?aid=28352
http://www.wu0553.com/m/view.php?aid=28340
http://www.wu0553.com/m/view.php?aid=28329
http://www.wu0553.com/m/view.php?aid=28317
http://www.wu0553.com/m/view.php?aid=28311
http://www.wu0553.com/m/view.php?aid=28304
http://www.wu0553.com/m/view.php?aid=28294
http://www.wu0553.com/m/view.php?aid=28285

http://www.wu0553.com/news/20190924/28894.html
http://www.wu0553.com/news/20190924/28893.html
http://www.wu0553.com/news/20190924/28892.html
http://www.wu0553.com/news/20190924/28891.html
http://www.wu0553.com/news/20190924/28890.html
http://www.wu0553.com/news/20190924/28889.html
http://www.wu0553.com/news/20190924/28888.html
http://www.wu0553.com/news/28785.html
http://www.wu0553.com/news/20190924/28784.html
http://www.wu0553.com/news/28782.html
http://www.wu0553.com/news/20190924/28780.html
http://www.wu0553.com/news/28777.html
http://www.wu0553.com/news/28773.html
http://www.wu0553.com/news/28770.html
http://www.wu0553.com/news/28767.html
http://www.wu0553.com/news/28765.html
http://www.wu0553.com/news/28761.html
http://www.wu0553.com/news/28758.html
http://www.wu0553.com/news/28755.html
http://www.wu0553.com/news/28754.html
http://www.wu0553.com/news/28752.html
http://www.wu0553.com/news/28751.html
http://www.wu0553.com/news/28748.html
http://www.wu0553.com/news/20190924/28746.html
http://www.wu0553.com/news/28743.html
http://www.wu0553.com/news/20190924/28741.html
http://www.wu0553.com/news/28739.html
http://www.wu0553.com/news/28738.html
http://www.wu0553.com/news/28735.html
http://www.wu0553.com/news/28733.html
http://www.wu0553.com/news/28732.html
http://www.wu0553.com/news/28729.html
http://www.wu0553.com/news/28726.html
http://www.wu0553.com/news/28723.html
http://www.wu0553.com/news/28718.html
http://www.wu0553.com/news/28716.html
http://www.wu0553.com/news/28715.html
http://www.wu0553.com/news/20190924/28713.html
http://www.wu0553.com/news/28710.html
http://www.wu0553.com/news/20190924/28707.html
http://www.wu0553.com/news/28704.html
http://www.wu0553.com/news/28702.html
http://www.wu0553.com/news/28701.html
http://www.wu0553.com/news/28698.html
http://www.wu0553.com/news/28695.html
http://www.wu0553.com/news/28693.html
http://www.wu0553.com/news/28691.html
http://www.wu0553.com/news/28687.html
http://www.wu0553.com/news/28685.html
http://www.wu0553.com/news/28683.html
http://www.wu0553.com/news/28681.html
http://www.wu0553.com/news/28679.html
http://www.wu0553.com/news/20190924/28676.html
http://www.wu0553.com/news/28674.html
http://www.wu0553.com/news/20190924/28672.html
http://www.wu0553.com/news/28670.html
http://www.wu0553.com/news/28669.html
http://www.wu0553.com/news/28667.html
http://www.wu0553.com/news/28664.html
http://www.wu0553.com/news/28662.html
http://www.wu0553.com/news/28659.html
http://www.wu0553.com/news/28658.html
http://www.wu0553.com/news/28657.html
http://www.wu0553.com/news/28656.html
http://www.wu0553.com/news/28655.html
http://www.wu0553.com/news/28654.html
http://www.wu0553.com/news/28653.html
http://www.wu0553.com/news/20190924/28652.html
http://www.wu0553.com/news/28647.html
http://www.wu0553.com/news/20190924/28642.html
http://www.wu0553.com/news/28637.html
http://www.wu0553.com/news/28630.html
http://www.wu0553.com/news/28625.html
http://www.wu0553.com/news/28620.html
http://www.wu0553.com/news/28614.html
http://www.wu0553.com/news/28608.html
http://www.wu0553.com/news/28601.html
http://www.wu0553.com/news/28591.html
http://www.wu0553.com/news/28581.html
http://www.wu0553.com/news/28570.html
http://www.wu0553.com/news/28559.html
http://www.wu0553.com/news/28549.html
http://www.wu0553.com/news/20190924/28539.html
http://www.wu0553.com/news/28531.html
http://www.wu0553.com/news/20190924/28520.html
http://www.wu0553.com/news/28511.html
http://www.wu0553.com/news/28499.html
http://www.wu0553.com/news/28487.html
http://www.wu0553.com/news/28476.html
http://www.wu0553.com/news/28469.html
http://www.wu0553.com/news/28460.html
http://www.wu0553.com/news/28450.html
http://www.wu0553.com/news/28442.html
http://www.wu0553.com/news/28436.html
http://www.wu0553.com/news/28431.html
http://www.wu0553.com/news/28427.html
http://www.wu0553.com/news/28422.html
http://www.wu0553.com/news/20190924/28419.html
http://www.wu0553.com/news/20190924/28408.html
http://www.wu0553.com/news/28398.html
http://www.wu0553.com/news/28385.html
http://www.wu0553.com/news/28376.html
http://www.wu0553.com/news/28364.html
http://www.wu0553.com/news/28352.html
http://www.wu0553.com/news/28340.html
http://www.wu0553.com/news/28329.html
http://www.wu0553.com/news/28317.html
http://www.wu0553.com/news/28311.html
http://www.wu0553.com/news/28304.html
http://www.wu0553.com/news/28294.html
http://www.wu0553.com/news/28285.html
http://www.wu0553.com/news/20190924/28274.html
http://www.wu0553.com/news/28265.html
http://www.wu0553.com/news/20190924/28253.html
http://www.wu0553.com/news/28246.html
http://www.wu0553.com/news/28234.html
http://www.wu0553.com/news/28229.html
http://www.wu0553.com/news/28224.html
http://www.wu0553.com/news/28214.html
http://www.wu0553.com/news/28204.html
http://www.wu0553.com/news/28196.html
http://www.wu0553.com/news/28187.html
http://www.wu0553.com/news/28179.html
http://www.wu0553.com/news/28175.html
http://www.wu0553.com/news/28172.html
http://www.wu0553.com/news/28167.html


http://www.wu0553.com/m/view.php?aid=28274
http://www.wu0553.com/m/view.php?aid=28265
http://www.wu0553.com/m/view.php?aid=28253
http://www.wu0553.com/m/view.php?aid=28246
http://www.wu0553.com/m/view.php?aid=28234
http://www.wu0553.com/m/view.php?aid=28229
http://www.wu0553.com/m/view.php?aid=28224
http://www.wu0553.com/m/view.php?aid=28214
http://www.wu0553.com/m/view.php?aid=28204
http://www.wu0553.com/m/view.php?aid=28196
http://www.wu0553.com/m/view.php?aid=28187
http://www.wu0553.com/m/view.php?aid=28179
http://www.wu0553.com/m/view.php?aid=28175
http://www.wu0553.com/m/view.php?aid=28172
http://www.wu0553.com/m/view.php?aid=28167

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!