[关闭]
@wxf 2019-02-18T01:26:06.000000Z 字数 3310 阅读 612

Vue基础知识

Vue.js


环境搭建

版本信息:vue-cli@3、vue@2.5
  1. 安装vue-cli 3

    1. npm install -g @vue/cli
    2. vue -V
  2. 创建项目

    1. vue create <项目名>
  3. 启动项目

    1. cd 项目名 # 进入到刚才新建的项目中
    2. npm run serve

模板语法

  1. 指令
    v-html指令:会解析HTML元素
    v-if指令:控制元素是否显示
    v-else指令:
    v-for指令:

    1. <ul>
    2. <li v-for="(item, index) in list" :key="item">{{item}}、{{index}}</li>
    3. </ul>

    v-for指令分组用法:

    1. <ul>
    2. <template v-for="(item, index) in list">
    3. <li :key="item+index">Hello</li>
    4. <li :key="item">{{item}}</li>
    5. </template>
    6. </ul>
  2. 自定义指令
    查阅官方文档

    什么时候使用自定义指令?
    当一个功能没有复杂到用一个组件去做这件事情时可以使用自定义指令。即一个单向的数据过来后,想对这个数据做很多处理,它又没有上升为一个很复杂的过程但是也不是一个表达式能够解决的。那么此时使用自定义指令是最为合适的。

  3. 特性
    <div v-bind:class=""> ……</div>

  4. 表达式
    {{ok ? 'YES' : 'NO'}} 三元表达式
    {{message.split('').reverse().join('')}} 链式操作语句

  5. 修饰符
    <form v-on:submit.prevent="onSubmit"></form>
    .prevent就是一个修饰符,其作用就是阻止默认行为

  6. 缩写
    <div v-bind:class=""> ……</div> <div :class=""> ……</div>
    <a v-on:click="doSomething">……</a> <a @click="doSomething">……</a>

  7. 计算属性
    应用场景:具有依赖关系的数据监听
    代码示例:

    1. <template>
    2. <div>
    3. 我借你{{money}},你还我{{returnMoney}},还剩{{getMoney}}
    4. </div>
    5. </template>
    6. <script>
    7. export default {
    8. name:'HelloWorld',
    9. data(){
    10. return {
    11. money: 1000,
    12. returnMoney: 100
    13. }
    14. },
    15. computed:{
    16. getMoney:function () {
    17. return this.money - this.returnMoney
    18. }
    19. }
    20. }
    21. </script>

类与样式

事件处理

查阅官方文档

  1. 定义&缩写

  2. 内联写法

  3. 事件修饰符

了解组件

  1. props 组件参数传递 (父组件传参给子组件)

  2. slot 插槽再组件抽象设计中的应用

  1. # 父组件
  2. <com :age="age" @patch="msg">
  3. <h1 slot="a">我要加东西</h1>
  4. </com>
  5. # 子组件(添加插槽标识)
  6. <slot name="a"></slot>
  7. ……
  8. <slot name="b"></slot>
  1. 自定义事件 父子组件的通信方式 (子组件传参给父组件)
  1. # 父组件
  2. <com :age="age" @patch="msg"/>
  3. methods: {
  4. msg: function (d) {
  5. this.age++
  6. console.info(d);
  7. }
  8. }
  9. # 子组件
  10. <button type="button" name="button" @click="$emit('patch', 50)">发送数据到父组件</button>

路由基础

查阅官方文档

  1. 安装

    1. npm install vue-router
  2. 使用路由的三个步骤

    1. 1. 声明 [VueRouter文件 - router.js]
    2. import Vue from 'vue'
    3. import VueRouter from 'vue-router'
    4. Vue.use(VueRouter)
    5. 2. 配置
    6. 2.1 路由本身的配置,即path与页面之间的关系 [VueRouter 文件 - router.js]
    7. const routes = [
    8. {path: '/',component:pageA},
    9. {path: '/pageb',component:pageB},
    10. ]
    11. 2.2 配置实例化与导出,作用就是提供给Vue大对象实例化时使用 [VueRouter 文件 - router.js]
    12. const router = new VueRouter({
    13. routes
    14. })
    15. export default router
    16. 2.3 Vue大对象实例化 [main.js]
    17. new Vue({
    18. router
    19. }).$mount('#app')
    20. 3. 挂载 [public/index.html]
    21. <!-- 路由匹配到的组件将渲染在这里 -->
    22. <router-view></router-view>

    注意: 需要配置使用包含运行时编译器的 Vue 构建版本 [vue.config.js]

    1. module.exports = {
    2. runtimeCompiler: true
    3. }
  3. 页面跳转

    1. <!-- 使用 router-link 组件来导航. -->
    2. <router-link to="/">page A</router-link>
    3. <router-link to="/pageb">page B</router-link>

Vuex — 多组件间数据交互

Vuex 基础

Vuex 数据流概念

  1. Vuex 数据概念
    • (数据在哪?)State: 组件间公用的数据存放到State中,它不是一个全局变量,并不能在所有组件中直接操作它。需要通过Mutations进行操作。
    • (如何改变数据?)Mutations:用来直接操作State数据的,通过它来定义如何操作数据。
    • (何时改变数据?)Actions: 决定什么时候触发(Commit)改变State数据的行为。
  2. Vuex 数据与组件之间的结合关系
    Vue Components可以读取(Render)State中的数据,Vue Components通过接受用户行为去触发(Dispatch)Actions。
    image_1d3shk53tb1ntmn1pkbu6o10la9.png-98.8kB

Vuex 使用

  1. 安装

    1. npm install vuex
  2. 定义Vuex

    1. 1. 声明 [Vuex文件 - store.js]
    2. import Vue from 'vue'
    3. import Vuex from 'vuex'
    4. Vue.use(Vuex)
    5. 2. Vuex数据(statemutationsactions)定义并导出 [Vuex文件 - store.js]
    6. const state = {
    7. count:1
    8. }
    9. const mutations = {
    10. increment(state){ // 加法函数
    11. state.count++
    12. },
    13. decrement(state){ // 减法函数
    14. state.count--
    15. }
    16. }
    17. const actions={
    18. increment:({commit})=>{
    19. commit('increment')
    20. },
    21. decrement:({commit})=>{
    22. commit('decrement')
    23. }
    24. }
    25. export default new Vuex.Store({state, mutations, actions})
    26. 3. Vuex实例引入到Vue实例中 [main.js]
    27. new Vue({
    28. store
    29. }).$mount('#app')
  3. 使用Vuex
  1. <p>State数据:{{$store.state.count}}</p>
  2. <p>使用计算属性后:{{countPlusLocalState}}</p>
  3. <button type="button" name="button" @click="increment">增加</button>
  4. <button type="button" name="button" @click="decrement">删减</button>
  5. import { mapActions, mapState } from 'vuex'
  6. export default {
  7. data () {
  8. return {
  9. localCount : 100
  10. }
  11. },
  12. methods: mapActions([
  13. 'increment',
  14. 'decrement'
  15. ]),
  16. computed: mapState({
  17. countPlusLocalState (state) {
  18. return state.count + this.localCount
  19. }
  20. })
  21. }

Vuex 高级

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注