Purpose

Notes for Vue

分割线

v-divider

水平分割线,上下分割

v-seperator

竖直分割线,左右分割

v-if

参考: https://www.runoob.com/vue2/vue-if.html 作用: 控制可见性,是否插入对应的标签

  • 相关指令

    • v-else
    • v-else-if
  • 相似指令

    • v-show

v-show

控制可见性,是否插入对应的标签

v-for

循环,重复插入标签

  • 使用方式

    • v-for="site in sites"

      • sites 是列表,数组
    • v-for="value in object"

      • object 是一个对象
      • 遍历对象的值,注意不是 key
    • v-for="(value, key) in object"
    • v-for="(value, key, index) in object"
    • v-for="n in 10"

      • n –> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
  • eg:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    
    <div id="app">
      <ol>
        <li v-for="site in sites">
          {{ site.name }}
        </li>
      </ol>
    </div>
    
    <script>
     new Vue({
         el: '#app',
         data: {
             sites: [
                 { name: 'Runoob' },
                 { name: 'Google' },
                 { name: 'Taobao' }
             ]
         }
     })
    </script>

v-bind

把 html 的标签属性,绑定到 vue object 的 property

  • eg: <a v-bind:href="url">…</a>

    • 把 <a> 元素的 href 属性绑定到 相关的 vue object 的 url 属性

设置多个值

https://www.runoob.com/vue2/vue-class-style.html

用在 class 上

eg:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
<div class="static" v-bind:class="{'active': isActive, 'text_danger': hasError}">
</div>
// class='static active text_danger'


// * 另一种写法
<div id="app">
<div v-bind:class="classObject"></div>
</div>

<script>
new Vue({
    el: '#app',
    data: {
        classObject: {
            active: true,
            'text-danger': true
        }
    }
})
</script>
  • 'active' 是要设置的 class 的名称

    • vue.isActive = true, 就会设置class'active'
    • 否则,不设置 class='active'
  • 多个值,用逗号分开即可

用在 style 上

eg:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
// 内嵌写法
<div id="app">
    <div v-bind:style="{ color: activeColor, fontSize: fontSize + 'px' }">菜鸟教程</div>
</div>
// vue.activeColor, vue.fontSize

// 单个 style object
<div id="app">
  <div v-bind:style="styleObject">菜鸟教程</div>
</div>

// 多个 style object
<div id="app">
  <div v-bind:style="[baseStyles, overridingStyles]">菜鸟教程</div>
</div>

v-on

  • 用途:用于监听事件
  • 作用:把 javascript 函数绑定到 vue 模板的 事件上
  • eg: <a v-on:click="doSomething">…</a>

    • 这里 click 是事件
    • 把 vue object 的方法,绑定到 click 事件

指令 Directives

以 "v-" 开头的 vue 模板标签 属性

  • eg: v-if, v-bind, v-on

v-card

参考: https://blog.csdn.net/u013089490/article/details/83896439 作用: cards 卡片组件

  • v-card-title
  • v-card-text
  • v-card-action

    • 按钮的容器
  • v-card-media

    • 图片或视频的容器
  • v-divider

    • <v-divider/>: 分割线

v-once

update template just one time, 只更新 template 模板的内容一次

v-html

vue 插入 html,

  • 用法: <div v-html="vue_object_property"></div>
  • 注:

    • 存在原因: vue 在“{{ msg }}” 中插入的是纯文本,不能是 html,
    • 所以,需要用 v-html 插入 html 文本

v-model

https://www.runoob.com/vue2/vue-template-syntax.html

  • 用于表单

  • 用于双向数据绑定,元素如:input, 用户输入的数据更新,
  • eg: <input v-model="message">

    • 用户输入数据更新,对应 vue.message 也回被更新
    • vue.message 也是 input 元素展示的初始内容

表单类型

过滤器

  • 实质:过滤函数
  • eg:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    
    <div id="app">
      {{ message | capitalize }}
    </div>
    
    <script>
    new Vue({
        el: '#app',
        data: {
            message: 'runoob'
        },
        filters: {
            capitalize: function (value) {
                if (!value) return ''
                value = value.toString()
                return value.charAt(0).toUpperCase() + value.slice(1)
            }
        }
    })
    </script>
  • 使用方式

    • 参数与过滤器使用 “|” 连接

创建 Vue 实例

相关参数

  • el

    • 定位元素
  • data

    • 属性
  • methods

    • 方法
  • computed

    • 计算属性
    • 实际存放的是函数
  • filters

    • 过滤器函数
  • watch

    • 在对应属性发生改变时,自动调用关联的函数
  • components

组件

https://www.runoob.com/vue2/vue-component.html

类型

  • 全局组件

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    
    Vue.component('name', {template: '...',
    
                           // 和new Vue(..) 参数类似
                           data: {...},
                           methods: {...}})
    
    
    // * eg
    Vue.component('button-counter', {
      template: '<button v-on:click="incrementHandler">{{ counter }}</button>',
    
    
      el:..., // * 注意:不需要这个属性,因为肯定使用的template 元素
    
      data: function () {
        return {
          counter: 0
        }
      },
      methods: {
        incrementHandler: function () {
          this.counter += 1
          this.$emit('increment')
        }
      },
    })
  • 局部组件

    • 在 Vue.components 下注册的 template

      1
      2
      3
      4
      5
      6
      7
      
      new Vue(
      {
      el:..,
      data: {},
      component: {
      'child_name': child_template}
      })

向子组件,传参

使用 prop 属性

  • 注意:

    • 传递过来的变量,需要在 prop 属性中以列表形式声明
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
<div id="app">
  <child message="hello!"></child>
</div>

<script>
 // 注册
 Vue.component('child', {
     // 声明 props
     props: ['message'],
     // 同样也可以在 vm 实例中像 "this.message" 这样使用
     template: '<span>{{ message }}</span>'
 })
 // 创建根实例
 new Vue({
     el: '#app'
 })
</script>

自定义事件

https://www.runoob.com/vue2/vue-component.html

  • this.$emit("event")
  • this.$on("event")

布局

格栅 Grid

https://blog.csdn.net/u014008101/article/details/86633896?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.edu_weight&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.edu_weight

https://vuetifyjs.com/zh-Hans/components/grids/

  • 相关功能标签

    • v-container/v-layout/v-flex

    • v-row/v-col

      • vue 2.x 用法
      • v-col 属性

        • cols: 指定跨列
       1
       2
       3
       4
       5
       6
       7
       8
       9
      10
      11
      12
      13
      14
      15
      16
      
        <v-container>
          <v-row>
            <v-col cols="12" md="6" sm="12">
              <v-btn block class="primary">第一个按钮</v-btn>
            </v-col>
            <v-col cols="12" md="2" sm="4" >
              <v-btn block class="primary">第二个按钮</v-btn>
            </v-col>
            <v-col cols="12" md="2" sm="4">
              <v-btn block class="primary">第三个按钮</v-btn>
            </v-col>
            <v-col cols="12" md="2" sm="4">
              <v-btn block class="primary">第四个按钮</v-btn>
            </v-col>
          </v-row>
        </v-container>

v-chip

  • 属性

    • color: 标签颜色
    • text-color

v-tootip

  • 属性

    • 指定位置

      • bottom, top, left, right

v-card

处理图片等

v-avatar

头像

导航抽屉

v-navigator-drawer

导航抽屉按钮

v-app-bar-nav-icon

组合实现例子

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
  <template>
      <div>
          <nav>
              <v-app-bar app flat>
                  <v-app-bar-nav-icon class="grey--text" @click="drawer=!drawer"></v-app-bar-nav-icon>

                  <v-toolbar-title class="text-uppercase grey--text">
                      <span class="font-weight-light">Hello</span>
                      <span>Piconjo</span>
                  </v-toolbar-title>

                  <v-spacer></v-spacer>
            
                  <v-btn outlined class="mx-2" color="grey">
                      <span>LoveU</span>
                      <v-icon right>favorite</v-icon>
                  </v-btn>
                  <v-btn outlined class="mx-2" color="grey">
                      <span>Login</span>
                      <v-icon right>lock</v-icon>
                  </v-btn>
              </v-app-bar>

              <v-navigation-drawer v-model="drawer" app class="indigo">
                  <p>Test</p>
              </v-navigation-drawer>
          </nav>
      </div>
  </template>

  <script>
  export default {
      data(){
          return {
              drawer:false
          }
      }
  }
  </script>

列表

https://blog.csdn.net/Piconjo/article/details/105940855?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.edu_weight&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.edu_weight

  • 相关标签

    • v-list
    • v-list-item
    • v-list-action
    • v-list-content
    • v-list-title

       1
       2
       3
       4
       5
       6
       7
       8
       9
      10
      
        <v-list>
            <v-list-item>
                <v-list-item-action>
                    <v-icon class="white--text">home</v-icon>
                </v-list-item-action>
                <v-list-item-content>
                    <v-list-item-title class="white--text">Homepage</v-list-item-title>
                </v-list-item-content>
            </v-list-item>
        </v-list>

导入

在 vue 中, 带 "@" 导入代表从根目录导入 https://www.cnblogs.com/heson/p/10517754.html

  • @ 指的是 src 目录

组件名称问题

大小写问题

MainFrame 和 main-frame 等价

默认导入问题

在 components 目录下

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  components
  ├───chemical
  │       bridges.js
  │       ChemDoodleWeb-unpacked.js
  │       CrystalSelector.vue
  │       ElementBtn.vue
  │       elements.json
  │       helpers.js
  │       jsonstr(2).json
  │       MoleculeViewer.vue
  │       ThePeriodTable.vue
  │       UnitCell.vue
  └───core
          Background.vue
          BorderBox.vue
          Breadcrumbs.vue
          ControlBox.vue
          ControlBox.vue~
          Input.vue
          Titlebar.vue
          Titlebar.vue~
          WindowBtn.vue
  • 默认被自动导入,

    • 使用名称 chemical_bridges
    • 名称格式:parentFolderName_componentName

Vuex

mapState, mapActions, mapMutations