Vue入門十三、路由的傳參和取參

1、查詢參login?id=12345

配置:(傳參):to="{name:'login', query:{id:'loginid'}}"
獲取:(取參)this.$route.query.id

我們提供的服務有:成都網站設計、成都網站制作、外貿網站建設、微信公眾號開發、網站優化、網站認證、愛民ssl等。為上千企事業單位解決了網站和推廣的問題。提供周到的售前咨詢和貼心的售后服務,是有科學管理、有技術的愛民網站制作公司

2、路由參數

配置:(傳參):to="{name:'login', params:{id:'loginid'}}"
獲取:(取參)this.$route.params.id

3、路徑參數register/registerid/info

配置:(傳參):to="{name:'register', params:{id:'registerid'}}"
配置路由規則:
routes: [{path: '/register/:id/info', name: 'register', component: Register}]
獲取:(取參)this.$route.params.id

4、使用props直接取參

配置:(傳參):to="{name:'propsParams', params:{id:'propsParamsId'}}"
配置路由規則:
routes: [
// 通過props傳值
{path: '/propsParams', name: 'propsParams', props: true, component: PropsParams}]
獲取:(取參)

 var PropsParams = {
        template: `
                <div>傳參直接取值
                <br>
                    直接拿到ID:{{id}}
                </div>
            `,
        // 直接通過props取參
        props: ['id']
    }
5、通過JS進行傳參

this.$router.push({path: '/login', name: 'login', query: {id: 'loginidjs'}})

代碼示例:

query傳參:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div id="app"></div>
<script type="text/javascript" src="vue.js"></script>
<script type="text/javascript" src="vue-router.js"></script>
<script type="text/javascript">
    var Login = {
        template: `
                <div>登陸了
                    <br>
                    登陸ID:{{msg}}
                </div>
            `,
        data() {
            return {
                msg: ''
            }
        },
        created() {
            this.msg = this.$route.query.id
        }
    }

    // 安裝插件
    Vue.use(VueRouter);
    // 創建路由對象
    var router = new VueRouter({
        // 配置路由對象
        routes: [{path: '/login', name: 'login', component: Login}
        ]
    })
    new Vue({
            el: '#app',
            router,
            template: `
            <div>
                <router-link :to="{name:'login', query:{id:'loginid'}}">query傳參</router-link>
                <router-view></router-view>
</div>
        `
        }
    )
</script>
</body>
</html>

params傳參

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div id="app"></div>
<script type="text/javascript" src="vue.js"></script>
<script type="text/javascript" src="vue-router.js"></script>
<script type="text/javascript">
    var Register = {
        template: `
                <div>注冊了
                 <br>
                    注冊ID:{{msg}}
                </div>
            `,
        data() {
            return {
                msg: ''
            }
        },
        created() {
            this.msg = this.$route.params.id
        }
    }
    var PathParams = {
        template: `
                <div>路徑傳參
                 <br>
                    路徑ID:{{pat}}
                </div>
            `,
        data() {
            return {
                pat: ''
            }
        },
        created() {
            this.pat = this.$route.params.id
        }
    }
    // 安裝插件
    Vue.use(VueRouter);
    // 創建路由對象
    var router = new VueRouter({
        // 配置路由對象
        routes: [
            {path: '/register', name: 'register', component: Register}
        ]
    })
    new Vue({
            el: '#app',
            router,
            template: `
            <div>
                <router-link :to="{name:'register', params:{id:'registerid'}}">params傳參</router-link>
                <router-view></router-view>
</div>
        `
        }
    )
</script>

</body>
</html>

params路徑傳參

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div id="app"></div>
<script type="text/javascript" src="vue.js"></script>
<script type="text/javascript" src="vue-router.js"></script>
<script type="text/javascript">
    var Register = {
        template: `
                <div>注冊了
                 <br>
                    注冊ID:{{msg}}
                </div>
            `,
        data() {
            return {
                msg: ''
            }
        },
        created() {
            this.msg = this.$route.params.id
        }
    }
    var PathParams = {
        template: `
                <div>路徑傳參
                 <br>
                    路徑ID:{{pat}}
                </div>
            `,
        data() {
            return {
                pat: ''
            }
        },
        created() {
            this.pat = this.$route.params.id
        }
    }
    // 安裝插件
    Vue.use(VueRouter);
    // 創建路由對象
    var router = new VueRouter({
        // 配置路由對象
        routes: [
            {path: '/register/:id/info', name: 'register', component: Register}
        ]
    })
    new Vue({
            el: '#app',
            router,
            template: `
            <div>
                <router-link :to="{name:'register', params:{id:'registerid'}}">path傳參</router-link>
                <router-view></router-view>
</div>
        `
        }
    )
</script>

</body>
</html>

params使用props直接取參

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div id="app"></div>
<script type="text/javascript" src="vue.js"></script>
<script type="text/javascript" src="vue-router.js"></script>
<script type="text/javascript">
    var PropsParams = {
        template: `
                <div>傳參直接取值
                <br>
                    直接拿到ID:{{id}}
                </div>
            `,
        // 直接通過props取參
        props: ['id']
    }
    // 安裝插件
    Vue.use(VueRouter);
    // 創建路由對象
    var router = new VueRouter({
        // 配置路由對象
        routes: [
            // 通過props傳值
            {path: '/propsParams', name: 'propsParams', props: true, component: PropsParams}
        ]
    })
    new Vue({
            el: '#app',
            router,
            template: `
            <div>
                <router-link :to="{name:'propsParams', params:{id:'propsParamsId'}}">傳參直接取值</router-link>
                <router-view></router-view>
</div>
        `
        }
    )
</script>

</body>
</html>

js傳參

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div id="app"></div>
<script type="text/javascript" src="vue.js"></script>
<script type="text/javascript" src="vue-router.js"></script>
<script type="text/javascript">
    // 安裝插件
    Vue.use(VueRouter);
    // 創建路由對象
    var router = new VueRouter()
    // 創建路由對象
    new Vue({
            el: '#app',
            router,
            template: `
            <div>
                <button @click="goLogin">JS跳轉登錄頁面</button>
</div>
        `,
            methods: {
                goLogin() {
                    // 使用js路由跳轉貌似只能使用query傳參
                    this.$router.push({path: '/login', name: 'login', query: {id: 'loginidjs'}})
                }
            }
        }
    )
</script>

</body>
</html>

fullPath路由全路徑監聽

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div id="app"></div>
<script type="text/javascript" src="vue.js"></script>
<script type="text/javascript" src="vue-router.js"></script>
<script type="text/javascript">
    var Login = {
        template: `
                <div>登陸了
                    <br>
                    登陸ID:{{msg}}
                </div>
            `,
        data() {
            return {
                msg: ''
            }
        },
        created() {
            this.msg = this.$route.query.id
        }
    }

    // 安裝插件
    Vue.use(VueRouter);
    // 創建路由對象
    var router = new VueRouter({
        // 配置路由對象
        routes: [
            {path: '/login', name: 'login', component: Login}
        ]
    })
    new Vue({
            el: '#app',
            router,
            template: `
            <div>
                <router-link :to="{name:'login', query:{id:'loginid'}}">query傳參</router-link>
                <button @click="goLogin">js跳轉</button>
                // js跳轉和標簽跳轉同一頁面,路由相同而參數不同頁面ID不刷新問題
                // 使用全路徑監聽fullPath
                <router-view :key="$route.fullPath"></router-view>
</div>
        `,
            methods: {
                goLogin() {
                    this.$router.push({path: '/login', name: 'login', query: {id: 'loginidjs'}})
                }
            }
        }
    )
</script>

</body>
</html>

完整代碼示例:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div id="app"></div>
<script type="text/javascript" src="vue.js"></script>
<script type="text/javascript" src="vue-router.js"></script>
<script type="text/javascript">
    var Login = {
        template: `
                <div>登陸了
                    <br>
                    登陸ID:{{msg}}
                </div>
            `,
        data() {
            return {
                msg: ''
            }
        },
        created() {
            this.msg = this.$route.query.id
        }
    }
    var Register = {
        template: `
                <div>注冊了
                 <br>
                    注冊ID:{{msg}}
                </div>
            `,
        data() {
            return {
                msg: ''
            }
        },
        created() {
            this.msg = this.$route.params.id
        }
    }
    var PathParams = {
        template: `
                <div>路徑傳參
                 <br>
                    路徑ID:{{pat}}
                </div>
            `,
        data() {
            return {
                pat: ''
            }
        },
        created() {
            this.pat = this.$route.params.id
        }
    }
    var PropsParams = {
        template: `
                <div>傳參直接取值
                <br>
                    直接拿到ID:{{id}}
                </div>
            `,
        props: ['id']
    }
    // 安裝插件
    Vue.use(VueRouter);
    // 創建路由對象
    var router = new VueRouter({
        // 配置路由對象
        routes: [{path: '/login', name: 'login', component: Login},
            {path: '/register', name: 'register', component: Register},
            // 路徑傳參
            {path: '/pathParams/:id/info', name: 'pathParams', component: PathParams},
            // 直接取
            {path: '/propsParams', name: 'propsParams', props: true, component: PropsParams}
        ]
    })
    new Vue({
            el: '#app',
            router,
            template: `
            <div>
                <router-link :to="{name:'login', query:{id:'loginid'}}">query傳參</router-link>
                <router-link :to="{name:'register', params:{id:'registerid'}}">params傳參</router-link>
                <router-link :to="{name:'pathParams', params:{id:'123456'}}">path傳參</router-link>
                <router-link :to="{name:'propsParams', params:{id:'propsParamsId'}}">傳參直接取值</router-link>
                <button @click="goLogin">JS跳轉登錄頁面</button>
                <router-view :key="$route.fullPath"></router-view>
</div>
        `,
            methods: {
                goLogin() {
                    this.$router.push({path: '/login', name: 'login', query: {id: 'loginidjs'}})
                }
            }
        }
    )
</script>

</body>
</html>

當前標題:Vue入門十三、路由的傳參和取參
標題鏈接:http://m.kartarina.com/article26/pgcejg.html

成都網站建設公司_創新互聯,為您提供外貿網站建設ChatGPTGoogle手機網站建設網站導航網站收錄

廣告

聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯

成都網站建設公司
主站蜘蛛池模板: 日本无码一区二区三区白峰美| 日韩A无码AV一区二区三区| 九九无码人妻一区二区三区| 无码熟妇人妻AV影音先锋| 中文字幕无码免费久久9一区9| 国产精品无码无卡无需播放器| 日日麻批免费40分钟无码| 18禁超污无遮挡无码免费网站国产 | 亚洲日韩av无码| 亚洲色无码专区一区| 国产色无码专区在线观看| 蜜臀AV无码一区二区三区| 无码一区二区三区| 国精品无码一区二区三区在线蜜臀| 亚洲中文字幕久久无码| 中文无码久久精品| 真人无码作爱免费视频| 亚洲av无码一区二区三区天堂| 伊人久久大香线蕉无码麻豆| 麻豆亚洲AV永久无码精品久久| 久久国产精品无码网站| 蜜芽亚洲av无码一区二区三区 | 精品少妇人妻AV无码专区不卡| 国产AV一区二区三区无码野战| 成在人线AV无码免费| 在线看片福利无码网址| 97精品人妻系列无码人妻| 无码视频在线观看| 亚洲av无码乱码国产精品fc2| 亚洲无码精品浪潮| JLZZJLZZ亚洲乱熟无码| 熟妇人妻系列av无码一区二区| 国产成人无码区免费A∨视频网站| 人妻精品无码一区二区三区| 亚洲精品无码久久久久APP| 在线观看亚洲AV每日更新无码| 久久中文字幕无码一区二区| 男男AV纯肉无码免费播放无码| 免费无码午夜福利片| 免费无码又爽又刺激一高潮| 亚洲VA中文字幕无码一二三区|