javascript - vue localStorages相關(guān) 路由傳值頁(yè)面刷新后報(bào)錯(cuò)
問題描述
代碼相關(guān):
剛開始學(xué)習(xí)vue的菜雞一只,js基礎(chǔ)不是太好,有什么不對(duì)的地方盡管批評(píng)指出謝謝。
productList頁(yè)面進(jìn)行跳轉(zhuǎn),然后根據(jù)index值取本地json數(shù)組中的數(shù)據(jù)來展現(xiàn)不同的頁(yè)面數(shù)據(jù),點(diǎn)擊跳轉(zhuǎn)后沒什么問題,然后刷新之后取不到值了,提示
[Vue warn]: Error in data(): 'SyntaxError: Unexpected token u in JSON at position 0'
搜了錯(cuò)誤信息的解釋但是還是不太理解,我是按慕課的一個(gè)vue的基礎(chǔ)教學(xué)里面保存localStorages的方法來的,是哪里寫錯(cuò)了嗎?
<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 頁(yè)面<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格式錯(cuò)誤首先你去判斷一下錯(cuò)誤出在哪里
先把mounted全部注釋
看看會(huì)不會(huì)報(bào)錯(cuò)
然后一條一條的加進(jìn)去
localStorage的值如果你使用的chrome,打開f12在application那里就能看到
大概率出現(xiàn)的原因是,你在一json格式保存數(shù)據(jù)之前先獲取了一個(gè)非json數(shù)據(jù),然后json.parse就報(bào)錯(cuò)了
相關(guān)文章:
1. node.js - mongodb查找子對(duì)象的名稱為某個(gè)值的對(duì)象的方法2. html5 - datatables 加載不出來數(shù)據(jù)。3. 運(yùn)行python程序時(shí)出現(xiàn)“應(yīng)用程序發(fā)生異常”的內(nèi)存錯(cuò)誤?4. docker 下面創(chuàng)建的IMAGE 他們的 ID 一樣?這個(gè)是怎么回事????5. 利用IPMI遠(yuǎn)程安裝centos報(bào)錯(cuò)!6. 前端 - @media query 使用出現(xiàn)的問題?7. javascript - 在 model里定義的 引用表模型時(shí),model為undefined。8. 測(cè)試自動(dòng)化html元素選擇器元素ID或DataAttribute [關(guān)閉]9. javascript - QQ第三方登錄的問題10. html5和Flash對(duì)抗是什么情況?

網(wǎng)公網(wǎng)安備