久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

javascript - VUE2.0 切換詳情頁(yè)數(shù)據(jù)

瀏覽:88日期:2023-03-23 11:27:19

問題描述

列表頁(yè)點(diǎn)擊到詳情可以正常根據(jù)id切換詳情內(nèi)容列表頁(yè):click函數(shù)添加 this.$router.push({ name: ’detail’, params: { id: id }});詳情接收傳遞過來的id this.$route.params.id,

列表頁(yè)右欄做了個(gè)導(dǎo)航(熱門文章),點(diǎn)擊熱門文章切換詳情內(nèi)容問題是:地址欄:xx/detail/id可以正常傳遞,詳情內(nèi)容沒變化正常hash有變化就應(yīng)該更改對(duì)應(yīng)的詳情數(shù)據(jù),熱門文章點(diǎn)擊,雖然hash變了,詳情頁(yè)面只加載了一次哪位vue大神可以給講下原因啊

具體三個(gè)頁(yè)面的代碼:APP.vue

<template> <p id='app'> <router-view></router-view> </p> <aside> <hotList></hotList> </aside></template><script type='text/ecmascript-6'> import Vue from ’vue’ import artList from ’./components/artList.vue’ import hotList from ’./components/hotList.vue’ export default { name:’app’, components: { hotList, artList } }</script>

hotList.vm ,,hotList.vm和artList.vm的代碼邏輯一樣的

<template> <p class='hotlist'> <ul> <li v-for='(item,index) in items' @click='goDetail(item.id)'> {{ item.title }} </li> </ul> </p></template><script type='text/ecmascript-6'> export default { name:’hotlist’, data () { return {items: null, } }, mounted: function(){ this.$http.get(’/api/list’).then( function (response) {this.items = response.data }, function(error) {console.log(error) }) }, methods:{ goDetail(id){this.$router.push({ name: ’detail’, params: { id: id }}); }, } }</script>

detail.vue

<template> <p class='detail'> <h2>{{detail.title}}</h2> <p>{{ detail.description }}</p> </p></template><script type='text/ecmascript-6'> export default { name:’detail’, data () { return {listId: this.$route.params.id,detail: {}, } }, mounted: function(){ this.getDetail(); }, methods: { getDetail(){this.$http.get(’/api/list/’ + this.listId) .then(function (res) { this.detail = res.data.id ? res.data : JSON.parse(res.request.response); }.bind(this)) .catch(function (error) { console.log(error); }); }, } }</script>

路由:

import artListfrom ’../components/artList.vue’import detail from ’../components/detail.vue’const router = new VueRouter({ routes:[ { path:’/home’, name: ’home’, component: artList, }, { path: ’/home/artList/detail/:id’, name: ’detail’, component: detail, } ] }); export default router;

問題解答

回答1:

初步估計(jì)問題出在detail.vue組件中。你的detail.vue的listId項(xiàng)的賦值出現(xiàn)了問題,嘗試這樣試一下:

export default { data () {return { listId: ’’} },mounted () {// 1.組件初步加載時(shí)賦值一次this.listId = this.$route.params.id; },watch: {’$route’: function () { //2. $route發(fā)生變化時(shí)再次賦值listId this.listId = this.$route.params.id;} }}

這樣組件初次加載的時(shí)候可以保證拿到正確的路由參數(shù),在路由發(fā)生變化的時(shí)候也可以正確的拿到路由參數(shù)。

標(biāo)簽: JavaScript
相關(guān)文章:
主站蜘蛛池模板: 仪陇县| 武强县| 额尔古纳市| 台前县| 柏乡县| 永年县| 乐平市| 寿宁县| 东丰县| 聂拉木县| 兴文县| 塔河县| 阿拉尔市| 湘阴县| 桐乡市| 富源县| 秦皇岛市| 宣恩县| 独山县| 霍州市| 章丘市| 申扎县| 天水市| 泗洪县| 乐至县| 闻喜县| 台安县| 泰宁县| 漳州市| 沾化县| 罗山县| 宽甸| 甘泉县| 团风县| 固镇县| 白山市| 九龙县| 扶余县| 慈利县| 奉贤区| 韶关市|