javascript - vue localStorages相關 路由傳值頁面刷新后報錯
問題描述
代碼相關:
剛開始學習vue的菜雞一只,js基礎不是太好,有什么不對的地方盡管批評指出謝謝。
productList頁面進行跳轉,然后根據index值取本地json數組中的數據來展現不同的頁面數據,點擊跳轉后沒什么問題,然后刷新之后取不到值了,提示
[Vue warn]: Error in data(): 'SyntaxError: Unexpected token u in JSON at position 0'
搜了錯誤信息的解釋但是還是不太理解,我是按慕課的一個vue的基礎教學里面保存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那里就能看到
大概率出現的原因是,你在一json格式保存數據之前先獲取了一個非json數據,然后json.parse就報錯了
相關文章:
1. windows誤人子弟啊2. mysql優化 - MySQL如何為配置表建立索引?3. 實現bing搜索工具urlAPI提交4. 關于mysql聯合查詢一對多的顯示結果問題5. 數據庫 - Mysql的存儲過程真的是個坑!求助下面的存儲過程哪里錯啦,實在是找不到哪里的問題了。6. 我在網址中輸入localhost/abc.php顯示的是not found是為什么呢?7. 如何用筆記本上的apache做微信開發的服務器8. python - linux怎么在每天的凌晨2點執行一次這個log.py文件9. MySQL主鍵沖突時的更新操作和替換操作在功能上有什么差別(如圖)10. 冒昧問一下,我這php代碼哪里出錯了???
