电竞比分网-中国电竞赛事及体育赛事平台

分享

[javascript] cdn模式下vue和vue-router實現(xiàn)路由

 Coder編程 2021-09-18

案例大部分都是用npm模式的,現(xiàn)在這個是使用cdn模式的更符合后端開發(fā)

 

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

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

    <script src="https://cdn./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變量 , 跳轉的時候可以帶著參數(shù) , 獲取到參數(shù)

//首頁組件
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;
    },
})

 

  

 

    本站是提供個人知識管理的網(wǎng)絡存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權內(nèi)容,請點擊一鍵舉報。
    轉藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約