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ù)來(lái)展現(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的方法來(lái)的,是哪里寫錯(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. python - 獲取到的數(shù)據(jù)生成新的mysql表2. javascript - js 對(duì)中文進(jìn)行MD5加密和python結(jié)果不一樣。3. mysql里的大表用mycat做水平拆分,是不是要先手動(dòng)分好,再配置mycat4. window下mysql中文亂碼怎么解決??5. sass - gem install compass 使用淘寶 Ruby 安裝失敗,出現(xiàn) 4046. python - (初學(xué)者)代碼運(yùn)行不起來(lái),求指導(dǎo),謝謝!7. 為啥不用HBuilder?8. python - flask sqlalchemy signals 無(wú)法觸發(fā)9. python的文件讀寫問題?10. 為什么python中實(shí)例檢查推薦使用isinstance而不是type?
