vuex网上记事本的仿写

使用vue-cli初始化一个项目:
vue init webpack note
npm install
看一下文件目录:这里写图片描述
接下来说一下各个文件的主要结构形式与功能:
store文件的形式:

import Vue from 'vue'
import Vuex from 'vuex'

Vue use(Vuex)

const state = {
    //初始的我们需要用到的状态
    }
const getters = {
    //依赖于state的衍生出来的状态
    }
const mutations = {
    //改变状态的方法
    }
const actions = {
    //由mutation衍生出来的方法
    }
//暴漏借口,导出模块
export default new Vuex.store({
    state,
    getters,
    mutations,
    action
    })

组件.vue文件:

<template>
    //dom代码片
</template>
<script>
    expoet default{
        //vue 代码
        }
</script>

APP.vue:

<template>
    组件名
</template>
<script>
//引入组件
import EditNote from './components/editNode'
import ToolBar from './components/toolBar'
import NoteList from './components/noteList'

//暴漏出接口 
export default {
  components: {
    EditNote,
    ToolBar,
    NoteList
  },
  name: 'app'
}

main.js:

//引入store文件
import store from './vuex/store'

new Vue ({
    el:'#app',
    //将store引入实例里
    store,
    ...
    })

下面是全部的代码,选择性观看。。。
代码:
editNode.vue

<template>
  <div class="text-wrapper">
      <button @click="saveNote">save</button>
      <textarea class="text-input" @input="saveText" :value="text"></textarea>
  </div>
</template>
<script>
export default {
  data () {
    return {
      textInput: ''
    }
  },
  computed: {
    text () {
      this.textInput = this.$store.getters.activeNote.text
      if (this.$store.getters.activeNote.text === undefined) {
        return ''
      } else {
        return this.$store.getters.activeNote.text
      }
    }
  },
  methods: {
    saveText (e) {
    //   console.log(e.target.value)
      this.textInput = e.target.value
    //   console.log(this.textInput)
    },
    saveNote () {
      if (this.textInput) {
        this.$store.dispatch('edit_note', this.textInput)
      }
    }
  }
}
</script>
<style>
.text-wrapper{
    width:100%;
}
.text-input{
    width:100%;
    height:80%;
    border:none;
    outline:none;
    padding:20px;
    font-size:15px;
}
button {
    width:100%;
    background-color: darksalmon;
    color:white;
    border:none;
    outline: none;
}
</style>

noteList.vue:

<template>
  <div class="note-list">
      <h3>记事本</h3>
      <div class="list-wrapper">
          <div class="tab">
              <div :class="{active:showAll==true}" @click="showAllNotes">All Notes</div>
              <div :class="{active:showAll==false}" @click="showFavornotes">Favorites</div>
          </div>
          <ul class="show-all" v-if="showAll">
              <li v-for="item in favornotes" :key="11" :class="{activeNote:item==activeNote}" @click="clickNote(item)">
                  {{item.text}}
              </li>
          </ul>
          <ul class="favorites" v-else>
              <li v-for="item in favornotes" :key="22" class="note" :class="{activeNote:item==activeNote}" @click="clickNote(item)">
                  {{item.text}}
              </li>
          </ul>
      </div>
  </div>
</template>
<script>
export default {
  data () {
    return {
      showAll: true
    }
  },
  computed: {
    notes () {
      return this.$store.getters.notes
    },
    favornotes () {
      return this.$store.getters.notes.filter(note => {
        return note.favor
      })
    },
    activeNote () {
      return this.$store.getters.activeNote
    }
  },
  methods: {
    showAllNotes: function () {
      this.showAll = true
    },
    showFavornotes: function () {
      this.showAll = false
    },
    clickNote: function (item) {
      this.$store.dispath('set_activenote', item)
    }
  }
}
</script>

<style>
.note-list{
    width:300p x;
    display: flex;
    flex-direction: column;
    align-items: center;
    background-color: whitesmoke;
}
.list-wraper{
    display: flex;
    flex-direction: column;
    align-content:center;
}
.tab{
    margin:0 auto;
    margin-bottom:10px;
}
.tab div{
    cursor: pointer;
    display:inline-block;
    border:1px solid #ddd;
    border-radius: 1px;
    padding:2px 14px;
}
.active{
    background-color: darksalmon;
    color: white;
}
.show-all, .favorites{
    width:300px;
    margin: 0;
    padding: 0;
}
.show-all li, .favorites li{
    overflow: hidden;
    word-wrap: break-word;
    height: 50px;
    margin:0;
    list-style: none;
    border-bottom: 1px solid #ddd;
    padding: 5px 10px;
}
.activeNote{
    background-color:blanchedalmond;
}

</style>

toolBar.vue:

<template>
  <div class="note-list">
      <h3>记事本</h3>
      <div class="list-wrapper">
          <div class="tab">
              <div :class="{active:showAll==true}" @click="showAllNotes">All Notes</div>
              <div :class="{active:showAll==false}" @click="showFavornotes">Favorites</div>
          </div>
          <ul class="show-all" v-if="showAll">
              <li v-for="item in favornotes" :key="11" :class="{activeNote:item==activeNote}" @click="clickNote(item)">
                  {{item.text}}
              </li>
          </ul>
          <ul class="favorites" v-else>
              <li v-for="item in favornotes" :key="22" class="note" :class="{activeNote:item==activeNote}" @click="clickNote(item)">
                  {{item.text}}
              </li>
          </ul>
      </div>
  </div>
</template>
<script>
export default {
  data () {
    return {
      showAll: true
    }
  },
  computed: {
    notes () {
      return this.$store.getters.notes
    },
    favornotes () {
      return this.$store.getters.notes.filter(note => {
        return note.favor
      })
    },
    activeNote () {
      return this.$store.getters.activeNote
    }
  },
  methods: {
    showAllNotes: function () {
      this.showAll = true
    },
    showFavornotes: function () {
      this.showAll = false
    },
    clickNote: function (item) {
      this.$store.dispath('set_activenote', item)
    }
  }
}
</script>

<style>
.note-list{
    width:300px;
    display: flex;
    flex-direction: column;
    align-items: center;
    background-color: whitesmoke;
}
.list-wraper{
    display: flex;
    flex-direction: column;
    align-content:center;
}
.tab{
    margin:0 auto;
    margin-bottom:10px;
}
.tab div{
    cursor: pointer;
    display:inline-block;
    border:1px solid #ddd;
    border-radius: 1px;
    padding:2px 14px;
}
.active{
    background-color: darksalmon;
    color: white;
}
.show-all, .favorites{
    width:300px;
    margin: 0;
    padding: 0;
}
.show-all li, .favorites li{
    overflow: hidden;
    word-wrap: break-word;
    height: 50px;
    margin:0;
    list-style: none;
    border-bottom: 1px solid #ddd;
    padding: 5px 10px;
}
.activeNote{
    background-color:blanchedalmond;
}

</style>

store.js:

<template>
  <div class="note-list">
      <h3>记事本</h3>
      <div class="list-wrapper">
          <div class="tab">
              <div :class="{active:showAll==true}" @click="showAllNotes">All Notes</div>
              <div :class="{active:showAll==false}" @click="showFavornotes">Favorites</div>
          </div>
          <ul class="show-all" v-if="showAll">
              <li v-for="item in favornotes" :key="11" :class="{activeNote:item==activeNote}" @click="clickNote(item)">
                  {{item.text}}
              </li>
          </ul>
          <ul class="favorites" v-else>
              <li v-for="item in favornotes" :key="22" class="note" :class="{activeNote:item==activeNote}" @click="clickNote(item)">
                  {{item.text}}
              </li>
          </ul>
      </div>
  </div>
</template>
<script>
export default {
  data () {
    return {
      showAll: true
    }
  },
  computed: {
    notes () {
      return this.$store.getters.notes
    },
    favornotes () {
      return this.$store.getters.notes.filter(note => {
        return note.favor
      })
    },
    activeNote () {
      return this.$store.getters.activeNote
    }
  },
  methods: {
    showAllNotes: function () {
      this.showAll = true
    },
    showFavornotes: function () {
      this.showAll = false
    },
    clickNote: function (item) {
      this.$store.dispath('set_activenote', item)
    }
  }
}
</script>

<style>
.note-list{
    width:300px;
    display: flex;
    flex-direction: column;
    align-items: center;
    background-color: whitesmoke;
}
.list-wraper{
    display: flex;
    flex-direction: column;
    align-content:center;
}
.tab{
    margin:0 auto;
    margin-bottom:10px;
}
.tab div{
    cursor: pointer;
    display:inline-block;
    border:1px solid #ddd;
    border-radius: 1px;
    padding:2px 14px;
}
.active{
    background-color: darksalmon;
    color: white;
}
.show-all, .favorites{
    width:300px;
    margin: 0;
    padding: 0;
}
.show-all li, .favorites li{
    overflow: hidden;
    word-wrap: break-word;
    height: 50px;
    margin:0;
    list-style: none;
    border-bottom: 1px solid #ddd;
    padding: 5px 10px;
}
.activeNote{
    background-color:blanchedalmond;
}

</style>

app.vue:

<template>
  <div class="note-list">
      <h3>记事本</h3>
      <div class="list-wrapper">
          <div class="tab">
              <div :class="{active:showAll==true}" @click="showAllNotes">All Notes</div>
              <div :class="{active:showAll==false}" @click="showFavornotes">Favorites</div>
          </div>
          <ul class="show-all" v-if="showAll">
              <li v-for="item in favornotes" :key="11" :class="{activeNote:item==activeNote}" @click="clickNote(item)">
                  {{item.text}}
              </li>
          </ul>
          <ul class="favorites" v-else>
              <li v-for="item in favornotes" :key="22" class="note" :class="{activeNote:item==activeNote}" @click="clickNote(item)">
                  {{item.text}}
              </li>
          </ul>
      </div>
  </div>
</template>
<script>
export default {
  data () {
    return {
      showAll: true
    }
  },
  computed: {
    notes () {
      return this.$store.getters.notes
    },
    favornotes () {
      return this.$store.getters.notes.filter(note => {
        return note.favor
      })
    },
    activeNote () {
      return this.$store.getters.activeNote
    }
  },
  methods: {
    showAllNotes: function () {
      this.showAll = true
    },
    showFavornotes: function () {
      this.showAll = false
    },
    clickNote: function (item) {
      this.$store.dispath('set_activenote', item)
    }
  }
}
</script>

<style>
.note-list{
    width:300px;
    display: flex;
    flex-direction: column;
    align-items: center;
    background-color: whitesmoke;
}
.list-wraper{
    display: flex;
    flex-direction: column;
    align-content:center;
}
.tab{
    margin:0 auto;
    margin-bottom:10px;
}
.tab div{
    cursor: pointer;
    display:inline-block;
    border:1px solid #ddd;
    border-radius: 1px;
    padding:2px 14px;
}
.active{
    background-color: darksalmon;
    color: white;
}
.show-all, .favorites{
    width:300px;
    margin: 0;
    padding: 0;
}
.show-all li, .favorites li{
    overflow: hidden;
    word-wrap: break-word;
    height: 50px;
    margin:0;
    list-style: none;
    border-bottom: 1px solid #ddd;
    padding: 5px 10px;
}
.activeNote{
    background-color:blanchedalmond;
}

</style>

main.js:

// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import store from './vuex/store'

Vue.config.productionTip = false

/* eslint-disable no-new */
new Vue({
  el: '#app',
  store,
  router,
  template: '<App/>',
  components: { App }
})
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值