1. 程式人生 > 實用技巧 >Vue路由程式設計式的導航 以及vue路由History 模式 hash 模式

Vue路由程式設計式的導航 以及vue路由History 模式 hash 模式

main.js

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


//引入公共的scss   注意:建立專案的時候必須用scss

import './assets/css/basic.scss';   




//請求資料


import VueResource from 'vue-resource';
Vue.use(VueResource);




import VueRouter from 'vue-router';

Vue.use(VueRouter);

//1.建立元件


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

import News from 
'./components/News.vue'; import Content from './components/Content.vue'; //2.配置路由 注意:名字 const routes = [ { path: '/home', component: Home }, { path: '/news', component: News,name:'news' }, { path: '/content/:aid', component: Content }, /*動態路由*/ { path: '*', redirect: '/home' } /*預設跳轉路由*/ ] //3.例項化VueRouter 注意:名字
const router = new VueRouter({ mode: 'history', /*hash模式改為history*/ routes // (縮寫)相當於 routes: routes }) //4、掛載路由 new Vue({ el: '#app', router, render: h => h(App) }) //5 <router-view></router-view> 放在 App.vue

app.vue

<template>


  <div id="app"> 

    <header class="header">

      <router-link to="/home">首頁</router-link>
      <router-link to="/news">新聞</router-link>

    </header>

    <hr>

       <router-view></router-view>

  </div>
</template>

<script>


/*
1、不同路由傳值:動態路由 1、配置動態路由 routes: [ // 動態路徑引數 以冒號開頭 { path: '/user/:id', component: User } ] 2、在對應的頁面 this.$route.params獲取動態路由的值 */ export default { data () { return { msg:'你好vue' } } } </script> <style lang="scss"> .header{ height:4.4rem; background:#000; color:#fff; line-height:4.4rem; text-align:center; a{ color:#fff; padding:0 2rem } } </style>

News.vue

<template>    
    <div id="news">    
       我是新聞元件   


     <ul class="list">
        <li v-for="(item,key) in list">
             <router-link :to="'/content/'+item.aid">{{item.title}}</router-link>
        </li>
     </ul>
          
    </div>

</template>


<script>

    export default{
        data(){
            return {               
               msg:'我是一個新聞元件'  ,    
               list:[]        
            }
        },
        methods:{

            requestData(){

                //jsonp請求的話  後臺api介面要支援jsonp

                var api='http://www.phonegap100.com/appapi.php?a=getPortalList&catid=20&page=1';

                this.$http.jsonp(api).then((response)=>{

                   console.log(response);

                   //注意:用到this要注意this指向

                   this.list=response.body.result;


                },function(err){

                        console.log(err);
                })
            }
        },
        mounted(){

            this.requestData();
        }
    }

</script>

<style lang="scss" scoped>
    
    .list{

        li{
            height:3.4rem;

            line-height:3.4rem;

            boder-bottom:1px solid #eee;

            font-size:1.6rem;

            a{

                color:#666;

                
            }
        }
    }

</style>

home.vue

<template>
    <!-- 所有的內容要被根節點包含起來 -->
    <div id="home">    
       我是首頁元件


        <button @click="goNews()">通過js跳轉到新聞頁面</button>
       
    </div>
</template>


<script>
    export default{
        data(){
            return {               
               msg:'我是一個home元件'
             
            }
        },
        methods:{

            goNews(){


                // 注意:官方文件寫錯了


                //第一種跳轉方式

                // this.$router.push({ path: 'news' })


                // this.$router.push({ path: '/content/495' });







                //另一種跳轉方式

                    //   { path: '/news', component: News,name:'news' },


                    // router.push({ name: 'news', params: { userId: 123 }})


                    this.$router.push({ name: 'news'})


                

            }
        }
    }

</script>

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

content.vue

<template>
   
    <div id="content">    
        <h2>{{list.title}}</h2>

        <div v-html="list.content"></div>
    </div>
</template>


<script>
    


    export default{

        data(){

            return{

                msg:'資料',
                list:[]
            }
        },
        mounted(){

               // console.log(this.$route.params);  /*獲取動態路由傳值*/

                var aid=this.$route.params.aid;

                //呼叫請求資料的方法

                this.requestData(aid);

        },
        methods:{

            requestData(aid){

                //get請求如果跨域的話 後臺php java 裡面要允許跨域請求

                var api='http://www.phonegap100.com/appapi.php?a=getPortalArticle&aid='+aid;


                this.$http.get(api).then((response)=>{
                        console.log(response);

                        this.list=response.body.result[0];

                },(err)=>{

                    console.log(err)
                })
            }
        }

    }
</script>

<style lang="scss">


#content{

    padding:1rem;

    line-height:2;

    img{

        max-width:100%;
    }
}
    
</style>