Vue 路由嵌套、資料請求、組件

來源:互聯網
上載者:User
<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title>Title</title></head><link rel="stylesheet" href="css/style.css"><body><div id = "app">    <div class="app" >        <a class="width"  ><router-link to="/tax">急速免稅</router-link> </a>        <a  class="width" ><router-link to="/classify"> 分類</router-link> </a>        <a  class="width" > <router-link to ="/cart">購物車</router-link></a>        <a  class="width" ><router-link to ="/my">我的</router-link> </a>    </div>    <router-view></router-view></div></body><template id = "tax">    <div >        <tax-child1></tax-child1>        <div v-for="name in names">            {{name}}        </div>        <span v-for="item in movies" >            <img  style="width: 150px;height:150px;" v-bind:src="item.avatars.large" alt="">            <span>{{item.name}}</span>        </span>       <div>           <ul style="float: left;margin-right: 50px;margin-left: 50px">               <li>                   <router-link :to="{path:'/tax/taxchild1',query:{id:12312313}}">route1</router-link>               </li>               <li>                   <router-link :to="{path:'/tax/taxchild2',query:{id:345353}}">route2</router-link>               </li>           </ul>           <router-view></router-view>       </div>    </div></template><script type="text/x-template" id = "classify">    <div>        <div>我是分類</div>        <button @click ="goCar">跳到購物車</button>    </div></script><script type="text/x-template" id = "cart">    <div>我是購物車</div></script><script type="text/x-template" id = "my">   <div>       <span>我的資料</span>   </div> </script><template id ="tax-child1">    <div>        <ul>            <li>tax-child1 01</li>            <li>tax-child1 02</li>            <li>tax-child1 03</li>        </ul>    </div></template><template id ="tax-child2">    <ul>        <li>tax-child2 01</li>        <li>tax-child2 02</li>        <li>tax-child2 03</li>    </ul></template><script type="text/javascript" src = "vue_js/vue.js"></script><script type="text/javascript" src = "vue_router/vue_router.js"></script><script type="text/javascript" src = "vue_js/resoure.js"></script><script>    /*import Vue from './vue_js/vue.js';    import Router from './vue_router/vue_router.js';    import resoure from './vue_js/resoure.js';*/    const testTax = Vue.component("test-tax",{        template:"#tax",        replace:true,        props:{        },        data:function(){            return {                movies:[],                names:["賭神","四面楚歌"]            }        },        mounted:function () {            console.log(this);            let that = this;            var url = "https://api.douban.com/v2/movie/subject/1764796";            this.$http.jsonp(url, {}, {            }).then(function(response) {                that.movies = response.body.casts;                console.log(that.movies);            }, function(response) {                // 這裡是處理錯誤的回調                console.log(response);            });        }    });    const testClassify =  Vue.component("test-classify",{        template:"#classify",        data:function(){            return {            }        },        methods:{            goCar:function (){                /*  route.go("/cart");*/                this.$router.push({ path: 'cart', query: { id: 12312312313 } });                /*this.$router.go({path:"cart"});拿不到值*/            }        }    });    const testCart  = Vue.component("test-cart",{        template:"#cart",        props:[],        data:function(){            return {            }        },        mounted:function(){            console.log(this.$route.query.id);        }    });    const testMy =  Vue.component("test-my",{        template:"#my",        props:[],        data:function(){            return {            }        },        mounted:function(){        }    });    const taxChild1 =  Vue.component("tax-child1",{        template:"#tax-child1",        data:function(){            return {            }        },        mounted:function(){            console.log(this.$route.query.id);        }    });    const taxChild2 =  Vue.component("tax-child2",{        template:"#tax-child2",        data:function(){            return {            }        }    });    const router = new VueRouter({        routes: [            {                path: '/tax',                component: testTax,                children:[                    {path:'/tax/taxchild1',component:taxChild1},                    {path:'/tax/taxchild2',component:taxChild2}                ]            },            {                path: '/classify',                component: testClassify            },            {                path: '/cart',                component: testCart            },            {                path: '/my',                component: testMy            },            { path: "", redirect: '/tax' }        ]    });    const app = new Vue({router}).$mount('#app')</script></html>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.