vue路由--靜態路由

vue的單頁面應用是基於路由和組件的,路由用於設定訪問路徑,並將路徑和組件映射起來。傳統的頁面應用,是用一些超連接來實現頁面切換和跳轉的。在vue-router單頁面應用中,則是路徑之間的切換,也就是組件的切換。javascript

首先在html中,引入vue-router.js和vue.js,用router-link觸發路由跳轉,router-link能夠像a標籤同樣使用和定義樣式css

router-view區域是路由匹配到的組件渲染的地方html

 

<script src="https://unpkg.com/vue/dist/vue.js"></script>
<script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
 
<div id="app">
  <h1>Hello App!</h1>
  <p>
    <!-- 使用 router-link 組件來導航. -->
    <!-- 經過傳入 `to` 屬性指定連接. -->
    <!-- <router-link> 默認會被渲染成一個 `<a>` 標籤 -->
    <router-link to="/foo">Go to Foo</router-link>
    <router-link to="/bar">Go to Bar</router-link>
  </p>
  <!-- 路由出口 -->
  <!-- 路由匹配到的組件將渲染在這裏 -->
  <router-view></router-view>
</div>

 

而後是js代碼vue

首先定義路由組件,組件能夠是簡單的組件(template簡單定義便可),也但是extend定義的複雜組件java

接下來定義路由映射表,就是定義路徑和組件之間的映射 git

而後使用路由映射表建立路由對象 github

最後使用路由對象建立vue對象,並掛載到指定元素vue-router

// 0. 若是使用模塊化機制編程,導入 Vue 和 VueRouter,要調用 Vue.use(VueRouter)
 
// 1. 定義(路由)組件。
// 能夠從其餘文件 import 進來
const Foo = { template: '<div>foo</div>' }
const Bar = { template: '<div>bar</div>' }
 
// 2. 定義路由
// 每一個路由應該映射一個組件。 其中"component" 能夠是
// 經過 Vue.extend() 建立的組件構造器,
// 或者,只是一個組件配置對象。
// 咱們晚點再討論嵌套路由。
const routes = [
  { path: '/foo', component: Foo },
  { path: '/bar', component: Bar }
]
 
// 3. 建立 router 實例,而後傳 `routes` 配置
// 你還能夠傳別的配置參數, 不過先這麼簡單着吧。
const router = new VueRouter({
  routes // (縮寫)至關於 routes: routes
})
 
// 4. 建立和掛載根實例。
// 記得要經過 router 配置參數注入路由,
// 從而讓整個應用都有路由功能
const app = new Vue({
  router// (縮寫)至關於 router: router
}).$mount('#app') // 如今,應用已經啓動了!

上例中,路由映射表實例名爲routes,在建立路由對象時能夠縮寫,若是不叫routes,好比叫routesa,則建立路由對象時必須寫routes:routesa編程

建立vue對象時,路由對象名也同樣,若是不叫router,也不能縮寫bootstrap

使用extend建立模板:

var todoItem = Vue.extend({
        data: function() {
            return {
                todoData: [
                    { id: 0, text: '蔬菜' },
                    { id: 1, text: '奶酪' },
                    { id: 2, text: '隨便其它什麼人吃的東西' }
                ]
            };
        },
        template: `
        <ul>
            <li v-for='(d, i) in todoData' :key="i">
                {{ d.text }}
            </li>
        </ul>
  `,

    });


    Home = { template: '<div>foo</div>' }
    About = { template: '<div>bar</div>' }

    routes = [
        { path: '/home', component: Home },
        { path: '/about', component: todoItem }
    ]

    router = new VueRouter({
        routes:routes // (縮寫)至關於 routes: routes
    });

    app = new Vue({
        router:router
    }).$mount('#app');

  

還能夠這樣寫template

<!DOCTYPE html>
<!-- saved from url=(0077)https://keepfool.github.io/vue-tutorials/06.Router/basic/basic_01.html#!/home -->
<html>

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>abc</title>
    <link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css">
    <link rel="stylesheet" href="./basic_01_files/custom.css">
</head>

<body>
    <div id="app">
        <div class="row">
            <div class="col-xs-offset-2 col-xs-8">
                <div class="page-header">
                    <h2>Router Basic - 01</h2>
                </div>
            </div>
        </div>
        <div class="row">
            <div class="col-xs-2 col-xs-offset-2">
                <div class="list-group">
                    
                    <router-link class="list-group-item" to="/home">Go to Foo</router-link>
                    <router-link class="list-group-item" to="/about">Go to Bar</router-link>
                </div>
            </div>
            <router-view></router-view>
        </div>
    </div>
    <template id="home">
        <div>
            <h1>Home</h1>
            <p>{{msg}}</p>
        </div>
    </template>
   
    <script src="https://cdn.bootcss.com/vue/2.4.2/vue.min.js"></script>
    <script src="https://cdn.bootcss.com/vue-router/2.7.0/vue-router.min.js"></script>
    <script>
    
    var todoItem = Vue.extend({
        data: function() {
            return {
                todoData: [
                    { id: 0, text: '蔬菜' },
                    { id: 1, text: '奶酪' },
                    { id: 2, text: '隨便其它什麼人吃的東西' }
                ]
            };
        },
        template: `
        <ul>
            <li v-for='(d, i) in todoData' :key="i">
                {{ d.text }}
            </li>
        </ul>
  `,

    });

    var t_test = Vue.extend({
        data:function(){
            return {
                msg:"hello,test"
            };
        },
        template:"#home"

        }

    );



    // Home = { template: '<div>foo</div>' }
    // About = { template: '<div>bar</div>' }

    routes = [
        { path: '/home', component: t_test },
        { path: '/about', component: todoItem }
    ]

    router = new VueRouter({
        routes: routes // (縮寫)至關於 routes: routes
    });

    app = new Vue({
        router: router
    }).$mount('#app');
    </script>
</body>

</html>

  若是不須要固定的導航連接,能夠把router-link放在模板裏面:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>abc</title>
    <script src="https://unpkg.com/vue/dist/vue.js"></script>
    <script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
</head>

<body>
    <div id="app">
        <h1>Hello App!</h1>
        
        <!-- 路由出口 -->
        <!-- 路由匹配到的組件將渲染在這裏 -->
        <router-view>
          
        </router-view>
    </div>
</body>
<script type="text/javascript">
// 0. 若是使用模塊化機制編程,導入 Vue 和 VueRouter,要調用 Vue.use(VueRouter)

// 1. 定義(路由)組件。
// 能夠從其餘文件 import 進來
const Foo = { template: '<router-link to="/bar">Go to Bar</router-link>' }
const Bar = { template: '<router-link to="/foo">Go to Foo</router-link>' }

// 2. 定義路由
// 每一個路由應該映射一個組件。 其中"component" 能夠是
// 經過 Vue.extend() 建立的組件構造器,
// 或者,只是一個組件配置對象。
// 咱們晚點再討論嵌套路由。
const routes = [
    { path: '/foo', component: Foo },
    { path: '/bar', component: Bar }
]

// 3. 建立 router 實例,而後傳 `routes` 配置
// 你還能夠傳別的配置參數, 不過先這麼簡單着吧。
const router = new VueRouter({
    routes // (縮寫)至關於 routes: routes
})

// 4. 建立和掛載根實例。
// 記得要經過 router 配置參數注入路由,
// 從而讓整個應用都有路由功能
const app = new Vue({
    router // (縮寫)至關於 router: router

}).$mount('#app') // 如今,應用已經啓動了!
</script>

</html>

 進去的時候打網址

xxx/xx.html#/foo 或 xxx/xx.html#/bar

就能夠實現foo和bar模板之間互相跳轉

也能夠設置默認路由:

const routes = [    { path: '/', component: Bar },    { path: '/foo', component: Foo },    { path: '/bar', component: Bar },    ]

相關文章
相關標籤/搜索