博客
关于我
Vue——进阶篇
阅读量:364 次
发布时间:2019-03-04

本文共 2546 字,大约阅读时间需要 8 分钟。

Vue之进阶篇

author:木子六日

学习视频来源于

文章目录

1. 环境

在开始开发前需要先搭建好开发环境。首先确保Node.js已安装。建议修改镜像地址为淘宝镜像源,以加快包裹下载速度。接着安装必要的开发工具:

  • 安装Webpack:npm install webpack -g
  • 安装Vue CLI:npm install vue-cli -g
  • 创建项目:vue create 项目名(选择router、Vuex和Babel插件,建议不选择ESLint以减少配置复杂度)
  • 可视化界面管理项目:vue ui
  • 项目部署:npm run build后将dist目录上传至服务器

2. Router

在单页面应用中,路由配置是必不可少的。确保在项目创建时选择了Router插件。

路由配置文件位于src/router/index.js。以下是一个典型的路由配置示例:

import Vue from 'vue'  import VueRouter from 'vue-router'  import Test1 from '../views/test1.vue'  import Test2 from '../views/test2.vue'  Vue.use(VueRouter)  const routes = [    { path: '/', redirect: '/test1' },    { path: '/test1', component: Test1 },    {      path: '/test2',      component: Test2,      children: [        { path: '', redirect: 'news' },        { path: 'news', component: () => import('../components/Test2News.vue') },        { path: 'sports', component: () => import('../components/Test2Sports.vue') }      ]    },    {      path: '/test3/:id',      component: () => import('../views/test3.vue')    },    { path: '/profile', component: () => import('../components/Profile.vue') }  ]  const router = new VueRouter({    routes  })

在组件中使用Router时,可以使用以下标签:

测试1      测试2      测试3      档案

对应的组件文件如下:

3. Vuex

Vuex用于集中管理组件状态,提供一个公共的数据池。确保在项目创建时选择了Vuex插件。

状态管理文件位于src/store/index.js。以下是一个简要的Vuex配置示例:

import Vue from 'vue'  import Vuex from 'vuex'  Vue.use(Vuex)  export default new Vuex.Store({    state: {      counter: 0    },    mutations: {      increment(state) {        state.counter++      },      decrement(state) {        state.counter--      },      incrementCount(state, count) {        state.counter += count      }    },    actions: {      aIncrement(context) {        setTimeout(() => {          context.commit('increment')        }, 1000)      }    }  })

在组件中使用Vuex状态:

{{ $store.state.counter }}       +      -      +5

建议安装Vue DevTools来更方便地调试Vuex状态。

4. Axios

Axios是Vue推荐的AJAX库,简洁易用,支持浏览器和Node.js环境。

安装命令:npm install axios --save

import axios from 'axios'  import App from './App.vue'  import router from './router'  import store from './store'  new Vue({    router,    store,    render: h => h(App)  }).$mount('#app')

Axios的使用示例:

axios({    url: 'http://123.207.32.32:8000/home/multidata'  }).then(res => {    console.log(res)  })

可根据需求配置全局参数:

axios.defaults.baseURL = 'http://123.207.32.32:8000'  axios.defaults.timeout = 2000

建议对Axios进行封装,便于维护:

export function request(config) {    return axios.create({      baseURL: 'http://123.207.32.32:8000'    }).call(config)  }

5. 其他工具

  • 建议使用Webpack或直接配置Webpack进行项目开发
  • Element UI是一个与Vue风格匹配的UI框架

转载地址:http://izee.baihongyu.com/

你可能感兴趣的文章
Notepad++在线和离线安装JSON格式化插件
查看>>
notepad++最详情汇总
查看>>
notepad++正则表达式替换字符串详解
查看>>
notepad如何自动对齐_notepad++怎么自动排版
查看>>
Notes on Paul Irish's "Things I learned from the jQuery source" casts
查看>>
Notification 使用详解(很全
查看>>
NotImplementedError: Cannot copy out of meta tensor; no data! Please use torch.nn.Module.to_empty()
查看>>
NotImplementedError: Could not run torchvision::nms
查看>>
nova基于ubs机制扩展scheduler-filter
查看>>
Now trying to drop the old temporary tablespace, the session hangs.
查看>>
nowcoder—Beauty of Trees
查看>>
np.arange()和np.linspace()绘制logistic回归图像时得到不同的结果?
查看>>
np.power的使用
查看>>
NPM 2FA双重认证的设置方法
查看>>
npm build报错Cannot find module ‘html-webpack-plugin‘解决方法
查看>>
npm build报错Cannot find module ‘webpack/lib/rules/BasicEffectRulePlugin‘解决方法
查看>>
npm build报错Cannot find module ‘webpack‘解决方法
查看>>