[关闭]
@File 2019-10-11T12:55:29.000000Z 字数 6844 阅读 142

vue 部署和组件化

web


相关连接

vue 官方文档
axios 官方文档
elementUi 官方文档

一、 vue-cli构建项目与打包部署

使用vue-cli能够快速的帮助我们构建项目,它就如同一个脚手架,提供了可选的模板。在使用vue-cli之前需要先安装nodejs。

1. 使用npm构建项目

  1. # 安装vue-cli,该步骤需要等一段时间
  2. npm i -g @vue/cli
  3. # 查看vue-cli的版本
  4. vue -V
  5. # 创建名为my-app的项目
  6. vue create my-app

2. 项目的结构介绍

3. 运行调试

  1. # 项目启动命令
  2. npm run serve

4. 项目的打包部署

执行命令:

  1. npm run build

将生成的dist目录下的文件放入到tomcat或者nginx下,启动服务器,即可访问。

二、 组件化开发

和原始写法差不多,只是把一个页面再拆分成多块,然后拼接起来

1. 代码结构

  1. <!-- html 内容 -->
  2. <template></template>
  3. <!-- js 代码块 -->
  4. <script>
  5. // vue 代码块,类似 new Vue({})
  6. export default {}
  7. </script>
  8. <!-- css 样式块 -->
  9. <style></style>

2. 引入模块

js实现:

  • import...from 引入文件
  • components: 注册组件
  1. <script>
  2. // import <别名> from <路径>
  3. import HelloWorld from './components/HelloWorld.vue'
  4. // vue 代码块,类似 new Vue({})
  5. export default {
  6. // 注册后可以在 template 中调用
  7. components: {
  8. // 调用格式: <HelloWorld></HelloWorld>
  9. HelloWorld
  10. }
  11. }
  12. </script>

html实现:

  • <HelloWorld/><hello-world/> 写法都可以
  1. <template>
  2. // 把引入的 html 引用到这里
  3. <hello-world/>
  4. </template>

3. 跨模块传参

父模块:

  • 给子类传参
    • 传字符串:<参数名>="<字符串>"
    • 传属性::<参数名>="<属性名>"
    • 传方法:@<参数名>="<方法名>"
  • 取子类参数
    • 声明取值:ref="<取值属性命名>"
    • vue调用:this.$refs.<取值属性>.<子类定义的属性名>
  1. <template>
  2. <!--
  3. 传字符串:<参数名>="<字符串>"
  4. 传属性::<参数名>="<属性名>"
  5. 传方法:@<参数名>="<方法名>"
  6. 声明赋值:ref="<取值属性命名>"
  7. -->
  8. <hello-world str="传入字符串" :name="name" @setName="setName" ref="helloWorld"/>
  9. </template>
  10. <!-- js -->
  11. <script>
  12. import HelloWorld from './components/HelloWorld.vue'
  13. export default {
  14. components: {
  15. HelloWorld
  16. },
  17. data() {
  18. // 设置属性
  19. return {name:'李大爷'}
  20. },
  21. methods: {
  22. // 设置方法
  23. setName(str) {
  24. this.name = str;
  25. // 配合 ref 直接获取 age 的参数
  26. this.age = this.$refs.helloWorld.age;
  27. }
  28. }
  29. }
  30. </script>

子模块:

  • props: 中定义接收的参数
  • this.$emit 在自定义方法中调用父类方法
  1. <template>
  2. <input v-once v-model="str">
  3. <input v-model="name">
  4. </template>
  5. <!-- js -->
  6. <script>
  7. export default {
  8. // 接收父模块参数
  9. props: {
  10. // 参数名: 参数类型
  11. str: String,
  12. name: String,
  13. age: 18
  14. },
  15. methods: {
  16. // 创建一个方法
  17. setName(str){
  18. // 方法中调用父模块传来的方法
  19. this.$emit('setName', str);
  20. }
  21. }
  22. }
  23. </script>

三、 pubsub 订阅与发布

1. 安装

  1. npm i pubsub-js -S

2. 发布方

  1. // 引入
  2. import PubSub from 'pubsub-js'
  3. export default {
  4. methods: {
  5. pub() {
  6. // 调用 publish('频道','发布的内容') 发布
  7. PubSub.publish('channel1', '张某人');
  8. }
  9. }
  10. }

3. 订阅方

  1. // 引入
  2. import PubSub from 'pubsub-js'
  3. export default {
  4. mounted() {
  5. // 调用 subscribe('频道',回调函数) 接收内容
  6. PubSub.subscribe('channel1', (key, value) => {
  7. // 订阅频道,channel1
  8. window.console.log(key);
  9. // 发布的内容
  10. window.console.log(value);
  11. });
  12. }
  13. }

四、 slot 插槽

1. 设立槽位

  1. <template>
  2. <div>
  3. <!-- 基本槽位 -->
  4. <slot name="title"></slot>
  5. <!-- 带属性的槽位 -->
  6. <slot name="item" v-bind="person"></slot>
  7. </div>
  8. </template>
  9. <!-- js -->
  10. <script>
  11. export default {
  12. data() {
  13. return {
  14. person: {age: 10, name: 'zhangsan'}
  15. }
  16. }
  17. }
  18. </script>

2. 绑定槽位

  1. <template>
  2. <List>
  3. <!-- v-slot 绑定 -->
  4. <template v-slot:title>
  5. <h2>插槽案例</h2>
  6. <button class="btn btn-danger btn-sm">点击</button>
  7. </template>
  8. <!-- # 绑定,并取 props 参数 -->
  9. <template #item="props">
  10. <h3>插槽属性</h3>
  11. <p>属性值:{{props}}</p>
  12. </template>
  13. </List>
  14. </template>
  15. <!-- js -->
  16. <script>
  17. // 引入带槽位的模块
  18. import List from './components/List.vue'
  19. export default {
  20. components: {
  21. List
  22. }
  23. }
  24. </script>

五、 axios 网络请求

axios官方文档

1. 安装

  1. npm i axios vue-axios -S

2. 配置全局请求地址

新建Base.vue文件,文件内容如下:

  1. <script>
  2. const BASE_URL = "http://localhost:8081"
  3. export default {
  4. BASE_URL
  5. }
  6. </script>

3. main.js配置

  1. import axios from 'axios'
  2. import VueAxios from 'vue-axios'
  3. import Base from './Base.vue'
  4. // 顺序有关系
  5. Vue.use(VueAxios, axios);
  6. // 配置基本地址
  7. axios.defaults.baseURL = Base.BASE_URL;

4. 发送请求

请求方式:
get: this.axios.get('请求地址')
post: this.axios.post('请求地址',{请求参数})
delete: this.axios.delete('请求地址')
axios: this.axios({自定义请求属性})

统一回调: .then(成功回调,失败回调)

  1. <script>
  2. export default {
  3. methods: {
  4. // post演示,举一反三
  5. post() {
  6. // 默认使用 json 格式提交参数
  7. this.axios.post('/addUser', {name:'李大爷', id: 10})
  8. .then(
  9. // 成功回调
  10. (resp) => {
  11. // resp.data 才是后端传过来的数据
  12. resp.data;
  13. },
  14. // 错误回调
  15. (error) => {}
  16. );
  17. },
  18. // axios 自定义普通表单提交
  19. axios() {
  20. this.axios({
  21. url: '/addUser'
  22. method: 'post',
  23. data: {
  24. name: '李大爷',
  25. id: 10
  26. }
  27. }).then(
  28. // 成功回调
  29. (resp) => {},
  30. // 错误回调
  31. (error) => {}
  32. )
  33. }
  34. }
  35. }
  36. </script>

六、 elementUi

elementUi官方文档

1. 安装

  1. npm i element-ui -S

2. main.js 配置

  1. import ElementUI from 'element-ui'
  2. import 'element-ui/lib/theme-chalk/index.css'
  3. Vue.use(ElementUI)

七、 VueRouter 路由

1. 安装

  1. npm i vue-router -S

2. 配置路由信息

  1. import Vue from 'vue'
  2. import Router from 'vue-router'
  3. const HOME = () => import('../views/Home');
  4. const ABOUT = () => import('../views/About');
  5. const HOME_INFO = () => import('../views/home/info');
  6. Vue.use(Router);
  7. export default new Router({
  8. mode: 'history',
  9. // 选中状态下默认添加的样式
  10. linkActiveClass: 'active',
  11. routes: [
  12. {
  13. path: '/home',
  14. component: HOME
  15. // 子路由
  16. children: [
  17. {
  18. // /:id 参数别名(非get提交(?id=2)时使用)
  19. path: '/home/info/:id'
  20. component: HOME_INFO,
  21. // 开启参数捕获(该属性get时默认false即可)
  22. props: true
  23. }
  24. ]
  25. },
  26. {
  27. path: '/about',
  28. component: ABOUT_PATH
  29. },
  30. // 根路径默认会重定向到/about路径
  31. {
  32. path: '/',
  33. redirect: '/about'
  34. }
  35. ]
  36. })

3. main.js 配置

  1. import Router from './router'
  2. new Vue({
  3. render: h => h(App),
  4. Router
  5. }).$mount('#app')

4. 一级路由设置

  1. <!-- 一级路由节点 -->
  2. <router-link to="/home">Home</router-link>
  3. <router-link to="/about">About</router-link>
  4. <!-- 路由页面展示节点 -->
  5. <router-view></router-view>

5. 二级路由设置

5.1 一级路由显示页面中设置二级路由节点

  1. <!-- 方式一 -->
  2. <router-link :to="'/user/'+user.id">{{user.name}}</router-link>
  3. <!-- 方式二 -->
  4. <router-link :to="`/user/${user.id}`">{{user.name}}</router-link>
  5. <!-- 方式三 -->
  6. <router-link :to="{path:'/user', query:{id:user.id}}">{{user.name}}</router-link>

5.2 二级路由显示页面中取参数

  1. export default {
  2. // 方法一 和 方法二的取值
  3. props: {
  4. id: String
  5. }
  6. // 方法三 的取值
  7. mounted() {
  8. this.id = this.$route.query.id; //用户刷新的时候有用
  9. }
  10. }

6. 编程式路由(逻辑跳转)

6.1 实现跳转

  1. this.$router.push({
  2. path: '/user',
  3. query: {id:id}
  4. });

6.2 取值

  1. this.$route.query.id;

7. 路由守卫

7.1 全局

  1. import Router from './router'
  2. // 全局路由守卫
  3. Router.beforeEach((to,from,next) =>{
  4. // to: 当前的地址
  5. // from: 来源地址
  6. // next: 放行
  7. next();
  8. });

7.2 局部

  1. export default {
  2. // 渲染该组件的对应路由被 confirm 前调用
  3. beforeRouteEnter (to, from, next) {
  4. // 组件实例还没创建,this 无效
  5. },
  6. // 导航离开当前路由时调用
  7. beforeRouteLeave (to, from, next) {}
  8. }

八、 Vuex 状态管理模式(集中式存储)

image_1dmt5vcjt4nd2661ri8mu76ogp.png-29.8kB

1. 安装

  1. npm i vuex -S

2. 配置 store

  1. // index.js 基本格式
  2. import Vue from 'vue'
  3. import Vuex from 'vuex'
  4. Vue.use(Vuex);
  5. // 普通属性
  6. const state = {};
  7. // 根据state计算出来的属性
  8. const getters = {};
  9. // 修改state属性的方法
  10. const mutations = {};
  11. // 调用 mutations 方法
  12. const actions = {};
  13. export default new Vuex.Store({
  14. state,
  15. actions,
  16. getters,
  17. mutations
  18. });

3. main.js 配置

  1. import store from './store'
  2. new Vue({
  3. // 引入store
  4. store,
  5. render: h => h(App)
  6. }).$mount("#app");

4. 调用

4.1 引入vuex对象

  1. // 按需引入
  2. import {mapState, mapActions, mapGetters, mapMutations} from 'vuex'

4.2 方法一:直接调用

  1. methods: {
  2. test() {
  3. // 执行 mutations 中的 setTest 方法
  4. // 可行但不规范:this.$store.commit('setTest',10);
  5. // 执行 actions 中的 setTest 方法
  6. this.$store.dispatch('setTest',10);
  7. // 取 state 中的属性 test
  8. window.console.log(this.$store.state.test);
  9. // 取 getters 中的 testNum
  10. window.console.log(this.$store.getters.testNum);
  11. }
  12. }

4.2 方法二:把对象引入到组件再调用

  1. computed: {
  2. // 引入属性
  3. ...mapState(['test']),
  4. // 引入计算属性
  5. ...mapGetters(['testNum'])
  6. },
  7. methods: {
  8. // 引入方法
  9. // 可行但不规范:...mapMutations(['setTest'])
  10. ...mapActions(['setTest']),
  11. test() {
  12. // 调用 actions 中的 setTest 方法
  13. this.setTest(10);
  14. // 取 state 中的属性 test
  15. window.console.log(this.test);
  16. // 取 getters 中的 testNum
  17. window.console.log(this.testNum);
  18. }
  19. }

5. 模块化

5.1 创建模块目录

  1. // store/demo/index.js
  2. const state = {};
  3. const getters = {};
  4. const mutations = {};
  5. const actions = {};
  6. export default ({
  7. state,
  8. actions,
  9. getters,
  10. mutations
  11. });

5.2 修改 store/index.js

  1. // index.js 基本格式
  2. import Vue from 'vue'
  3. import Vuex from 'vuex'
  4. // 引入 demo 模块
  5. import demo from './demo'
  6. Vue.use(Vuex);
  7. export default new Vuex.Store({
  8. // 引入 demo 模块
  9. modules: {
  10. demo
  11. }
  12. });

5.3 state 调用方式调整

getters、actions、mutations都被注册到全局上,和之前的调用方式一样。

  1. // 导入式调用
  2. ...mapState({test: state => state.demo.test});
  3. // 直接调用
  4. this.$store.state.demo.test;
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注