1. 程式人生 > >vue獲取內存中的值並寫入

vue獲取內存中的值並寫入

ont cli tor mod pan def use creat nsh

<template>
    <div class="container">
        <h3>發表評論</h3>
        <hr>
        <textarea placeholder="請輸入" maxlength="120" v-model="content"></textarea>
        <mt-button type="primary" size="large" @click="postComments">發表評論</mt-button>
        <div class="cmtlist" v-for
="(item,i) in list" :key="item.id"> <div class="cmtitem"> <div class="cmttitle">第{{i+1}}樓&nbsp;&nbsp;匿名用戶&nbsp;&nbsp;發表時間:{{item.id|dateFormat}}</div> <div class="cmtbody">{{item.content}}</div> </div> </div> <mt-button type="danger" size="large" plain>加載更多</mt-button> </div> </template> <script> export
default{ data(){ return { user:‘‘, content:‘‘, list:[{id:1,user:‘李白‘,content:‘天生我材必有用‘},], } }, methods:{ loadComments(){ var list = JSON.parse(localStorage.getItem(‘cmts‘)||‘[]‘) this.list= list }, postComments(){
var comment = {id:Date.now(),user:‘user111‘,content:this.content} var list = JSON.parse(localStorage.getItem(‘cmts‘)||‘[]‘) list.unshift(comment) localStorage.setItem(‘cmts‘,JSON.stringify(list)) this.content=‘‘ this.loadComments() }, }, created(){ this.loadComments() } } </script> <style> .container h3{ font-size: 18px; } textarea{ font-size:14px ; height: 85px; margin: 0; } .cmtlist{ margin: 10px 0; } .cmttitle{ background-color: #ccc; line-height: 30px; } .cmtitem{ font-size: 13px; } .cmtbody{ line-height: 35px; text-indent: 2em; } </style>

技術分享圖片

vue獲取內存中的值並寫入