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

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

javascript - vue localStorages相關(guān) 路由傳值頁面刷新后報錯

瀏覽:120日期:2023-03-06 16:12:02

問題描述

代碼相關(guān):

剛開始學(xué)習(xí)vue的菜雞一只,js基礎(chǔ)不是太好,有什么不對的地方盡管批評指出謝謝。

productList頁面進行跳轉(zhuǎn),然后根據(jù)index值取本地json數(shù)組中的數(shù)據(jù)來展現(xiàn)不同的頁面數(shù)據(jù),點擊跳轉(zhuǎn)后沒什么問題,然后刷新之后取不到值了,提示

[Vue warn]: Error in data(): 'SyntaxError: Unexpected token u in JSON at position 0'

搜了錯誤信息的解釋但是還是不太理解,我是按慕課的一個vue的基礎(chǔ)教學(xué)里面保存localStorages的方法來的,是哪里寫錯了嗎?

<li v-for='(item,index) in filterList' > <router-link :to='{ name: ’detail’, params: { id: index }}'> </router-link></li>

//store.jsconst STORAGE_KEY = ’epmobile’export default { fetch() {return JSON.parse(window.localStorage.getItem(STORAGE_KEY) || ’[]’) }, save(items) {window.localStorage.setItem(STORAGE_KEY, JSON.stringify(items)) }}

//detail 頁面<script>import Store from ’../store/store’export default { data() {return { articleList: ’’, index: Store.fetch()} }, mounted() {this.$nextTick(function () { this.index = this.$route.params.id this.get_articleList()}) }, watch: { index: { handler: function (index) { Store.save() }, deep: true } }, methods: {get_articleList: function () { this.$http.get(’/api/article.json’).then(response => {let res = response.dataif (res.status == 0) { this.articleList = res.result.articleList[this.index]} })} }}</script>

{ 'status': 0, 'result': {'articleList': [ {'title': '111','productImg': '/static/images/product_e/01.jpg','productText': 'xxxxx','companyInfo': { 'name': 'xxxx', 'url': 'xxxxx', 'boothNumber': 'xxxx'} }, {'title': '2222222222', 以下省略... }] }}

問題解答

回答1:

大概率是json格式錯誤首先你去判斷一下錯誤出在哪里

先把mounted全部注釋

看看會不會報錯

然后一條一條的加進去

localStorage的值如果你使用的chrome,打開f12在application那里就能看到

大概率出現(xiàn)的原因是,你在一json格式保存數(shù)據(jù)之前先獲取了一個非json數(shù)據(jù),然后json.parse就報錯了

標簽: JavaScript
相關(guān)文章:
主站蜘蛛池模板: 芷江| 南丰县| 崇仁县| 正宁县| 长岭县| 故城县| 南部县| 诸城市| 东乡| 镶黄旗| 三穗县| 浑源县| 惠水县| 离岛区| 宁国市| 青阳县| 伊川县| 托里县| 怀柔区| 建湖县| 胶南市| 古浪县| 壶关县| 岳西县| 赣榆县| 兴和县| 甘孜| 九龙城区| 宜君县| 祥云县| 荔浦县| 松滋市| 沂水县| 阿荣旗| 伽师县| 安阳县| 鹤庆县| 翼城县| 崇州市| 通河县| 彭山县|