javascript - vue localStorages相關(guān) 路由傳值頁面刷新后報(bào)錯(cuò)
問題描述
代碼相關(guān):
剛開始學(xué)習(xí)vue的菜雞一只,js基礎(chǔ)不是太好,有什么不對(duì)的地方盡管批評(píng)指出謝謝。
productList頁面進(jìn)行跳轉(zhuǎn),然后根據(jù)index值取本地json數(shù)組中的數(shù)據(jù)來展現(xiàn)不同的頁面數(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 頁面<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. javascript - 能否讓vue-cli的express修改express重啟服務(wù)2. java - 注解上的屬性可以傳遞嗎?3. 解決Android webview設(shè)置cookie和cookie丟失的問題4. android - 分享到微信,如何快速轉(zhuǎn)換成字節(jié)數(shù)組5. javascript - vue2.0中,$refs對(duì)象為什么用駝峰的方式獲取不到屬性?6. python bottle跑起來以后,定時(shí)執(zhí)行的任務(wù)為什么每次都重復(fù)(多)執(zhí)行一次?7. node.js - npm install全局安裝出錯(cuò),請(qǐng)問如何解決?謝謝!8. node.js - mac安裝mongodb第一次啟動(dòng)失敗9. javascript - nodejs使用mongoose連接數(shù)據(jù)庫(kù),使用post提交表單在后臺(tái),后臺(tái)處理后調(diào)用res.redirect()跳轉(zhuǎn)界面無效?10. node.js - npm一直提示proxy有問題
