[javascript] cdn模式下vue和vue-router實現路由


案例大部分都是用npm模式的,現在這個是使用cdn模式的更符合后端開發

 

html部分 , 注意template標簽 ,定義上的id

    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/vue-router/dist/vue-router.js"></script>

    <script src="https://cdn.jsdelivr.net/npm/jquery/dist/jquery.min.js"</script>


</head>
<body>
<div id="app" class="chatKfPageApp">
    <router-view></router-view>
</div>
<template id="chatKfIndex">
<div>111</div>
</template>
<template id="chatBox">
    <div>222</div>
</template>
</body>

<script src="/static/js/chat-kf-page.js?v=0.1.1"></script>
</html>

js部分 , 注意每個template對應一個組件 , 子template可以繼承父級的data變量 , 跳轉的時候可以帶着參數 , 獲取到參數

//首頁組件
var chatKfIndex = {
    data: function(){
        return {
            visitors: {},
        }
    },
    methods: {
    },
    created: function () {
    },
    template:$("#chatKfIndex").html()
};
//詳情組件
var chatKfBox = {
    data: function(){
        return {
            msgList: [],
            messageContent: "",
            face: [],
        }
    },
    methods: {
        init(){
            alert(this.$parent.socket);
            alert(this.$route.params.visitorId);
        },
    },
    created: function () {
        this.init();
    },
    template:$("#chatBox").html()
};
var routes = [
    { path: '/',component:chatKfIndex}, // 這個表示會默認渲染
    {path:'/chatKfBox/:visitorId',component:chatKfBox},
];
var router = new VueRouter({
    routes: routes
})

new Vue({
    router,
    el: '#app',
    data: function(){
        return{
            socket:null,
        }
    },
    created: function () {
        this.socket=3;
    },
})

 

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM