Vue

Vue单文件组件

基本用法

  • 以**.vue**结尾
  • template:组件的模块区域
  • script:业务逻辑区域
  • style:样式区域
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<template> 
<!-- 这里写定义VUE组件的模板内容 -->
</template>
<script>
// 这里用于定义Vue组件的业务逻辑
export default{
data:(){
return {} // 私有数据
},
methods:{
// 处理函数
},
// ...其他
}
</script>
// scoped是为了防止组件之间的样式冲突
<style scoped>
/*这里用来写样式*/
</style>

在webpack中使用vue

  1. 运行npm i vue -S安装
  2. src/index.js中通过**import Vue from ‘vue’**导入
  3. 创建vue的实例对象,并指定要控制的el区域
  4. 通过render函数渲染APP组件
1
2
3
4
5
6
7
8
9
10
11
// 导入Vue构造函数
import Vue from 'vue'
// 导入App根组件
import App from './components/App.vue'

const vm = new Vue({
// 制定要控制的页面区域
el:'#app',
// 通过render函数,把制定的组件渲染到el区域中
render:h=>h(App)
})

Vue脚手架

基本使用

  • Vue脚手架是用于快速生成Vue项目的基础架构官网
  • 安装npm install -g @vue/cli

交互式命令行创建项目

  • vue create 名字
    vue-1.png
    vue-2.png
    vue-3.png
    vue-4.png
    vue-5.png
    vue-6.png

图形化界面创建项目

  • vue ui
    vue-cli-1.png
    vue-cli-2.png
    vue-cli-3.png
    vue-cli-4.png
    vue-cli-5.png
    vue-cli-6.png
    vue-cli-7.png

文件分析

vue配置文件分析.png

  • 自定义配置package.json

    1
    2
    3
    4
    5
    6
    "vue":{
    "devServer":{
    "port":"8888", // 端口号为8888
    "open":true // 打包成功后自动打开浏览器
    }
    }
    • 不推荐使用这种配置,应该单独定义到vue.config.js配置文件中
  • 单独配置文件配置

    1. 在项目根目录创建vue.config.js
    2. 在该文件中进行配置,覆盖默认配置
    1
    2
    3
    4
    5
    6
    7
    8
    module.exports = {
    devServer: {
    // 自定义端口号
    port: 8888,
    // 自动打开浏览器
    open: true
    }
    }

Element-UI

基于命令方式安装

  1. 安装依赖npm i element-ui -S

  2. main.js导入Element-UI资源

    1
    2
    3
    4
    5
    6
    // 导入组件库
    import ElementUI from 'element-ui';
    // 导入组件相关样式
    import 'element-ui/lib/theme-chalk/index.css';
    // 配置Vue插件
    Vue.use(ElementUI);

基于图形化界面

  1. 运行vue ui
  2. 通过Vue项目管理器进行具体的项目配置
  3. 点击插件-》添加插件,进行插件查询面板
  4. 搜索vue-cli-plugin-element安装
  5. 配置插件,实现按需导入

element-ui.png

安装axios依赖

axios.png

Vuex

基本介绍

  • Vuex是实现组件全局状态(数据)管理的一种机制,可以方便的实现组件之间数据的共享
  • Vuex中的数据都是响应式的,能够实时保持数据与页面的同步

使用

  1. 安装vuex依赖包npm install vuex –save

  2. 导入vuex包

    1
    2
    import Vuex from 'vuex'
    Vue.use(Vuex)
  3. 创建store对象

    1
    2
    3
    4
    const store = new Vuex.Store({
    // state 中存放的就是全局共享的数据
    state:{ count:0 }
    })
  4. 将store对象挂载到vue实例

    1
    2
    3
    4
    5
    6
    new Vue({
    el: '#app',
    // 将创建的共享数据对象,挂载到Vue实例中
    // 所有的组件,就可以直接从store中获取全局的数据了
    store
    })

vuex1.png
vuex3.png
vuex4.png
vuex2.png

State

  • State提供唯一的公共数据源,所有共享的数据都要同意放到Store的State中进行存储

    1
    2
    3
    4
    // 创建store数据源,提供唯一公共数据
    const store = new Vuex.Store({
    state:{ 数据名称: 默认值 }
    })
  1. 组件访问State中数据的第一种方式

    1
    this.$store.state.全局数据名称
  2. 组件访问State中数据第二种方式

    1
    2
    3
    4
    5
    6
    // 1.从vuex中按需导入mapState函数
    import { mapState } from 'vuex'
    // 2.将全局数据,映射为当前组件的计算属性
    computed:{
    ...mapState(['全局数据名称'])
    }

Mutation

  • 使用Mutation修改变更Store中的数据

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    // 定义
    const store = new Vuex.Store({
    state:{
    count:0
    },
    mutations:{
    add(state){
    // 变更状态
    state.count++
    },
    // 传递参数
    add2(state, step){
    state.count += step
    }
    }
    })
  • 第一种方式

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    // 使用
    methods:{
    add(){
    // 触发motations的第一种方式,下面这个add是mutations中的方法名字
    this.$store.commit('add')
    },
    add2(){
    // 传递参数
    this.$store.commit('add2',3)
    }
    }
  • 第二种方式

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    // 1. 从vuex中按需导入 mapMutations 函数
    import { mapMutations } from 'vuex'

    // 2. 将指定的 mutations 函数,映射为当前组件的 methods 函数
    methods:{
    ...mapMutations(['add','add2']),
    addf(){
    this.add()
    },
    add2f(){
    // 传递参数
    this.add2(3)
    }
    }

Action

  • Action用于异步处理
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
// 定义
const store = new Vuex.Store({
state:{
count:0
},
mutations:{
add(state){
// 变更状态
state.count++
},
// 传递参数
add2(state, step){
state.count += step
}
},
actions:{
// 在actions中,不能直接修改state中的数据
// 必须通过 context.commit()触发某个mutation才行
addAsync(context){
setTimeout(()=>{
context.commit('add')
},1000)
},
// 传递参数
addAsync2(context, step){
setTimeout(()=>{
context.commit('add2', step)
},1000)
}
}
})
  1. 触发actions的第一种方法
1
2
3
4
5
6
7
8
9
// 使用
methods:{
add(){
this.$store.dispatch('addAsync')
},
add2(){
this.$store.dispatch('addAsync2', 5)
}
}
  1. 触发actions的第二种方法

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    // 1. 从vuex中按需导入 mapActions 函数
    import { mapActions } from 'vuex'

    // 2. 将指定的 mutations 函数,映射为当前组件的 methods 函数
    methods:{
    ...mapActions(['addAsync','addAsync2']),
    addf(){
    this.addAsync()
    },
    add2f(){
    // 传递参数
    this.addAsync2(3)
    }
    }

Getter

  • Getter不会修改原数据,用于对Store中的数据进行加工处理形成新的数据
  • Store中的数据发生变化,Getter的数据也会跟着变化
1
2
3
4
5
6
7
8
9
10
11
// 定义
const store = new Vuex.Store({
state:{
count: 0
},
getters:{
showNum: state=>{
return 'state中的数据为:'+state.count
}
}
})
  1. 获取第一种方式
1
this.$store.getters.名称
  1. 获取第二种方式
1
2
3
4
5
import { mapGetters } from 'vuex'

computed: {
...mapGetters(['showNum'])
}

相关文章

JavaScript基础

JavaScript高级

Jquery基础

Jquery高级

Bootstrap

Ajax和Json

DataTables

Sweetalert2

zTree使用

微信小程序