1,什么是node.js,以及npmcss
简单的来讲Node.js就是运行在服务端的JavaScript,是基于Chrome V8引擎的.npm是Node.js包的管理工具.vue
2,npm的安装和更新node
Node.js下载安装Node.js官网下载安装.npm自带的包管理工具.jquery
更新到npm指定版本:webpack
npm经常使用操做ios
以前咱们用jQuery或者Bootstrap用cdn或者直接手动下载并放入项目,并且要管理版本.有了npm,咱们管理本身的依赖包以及版本更加简单web
到本身项目下,进行下列命令:ajax
咱们的项目目录下会生成一个node_modules目录,咱们用npm下的包会在这个目录下.vuex
咱们全部的依赖信息放在package.json文件中,包括咱们全部的依赖以及版本vue-cli
若是咱们删掉node_modules目录,可使用npm i 来下载全部依赖.
3,npm经常使用配置
当咱们用npm init的时候用了参数-y, 若是不用-y咱们能够进行一些配置,在咱们的package.json文件中有不少配置项
4,webpack3
webpack:是一个模块打包器,他将根据模块的依赖关系静态分析,而后将这些模块按照指定的规则生产成静态资源,
4.1,安装和配置
webpack是跑在Node.js环境下的,因此肯定有node环境
安装方式:
4.2,entry和output
entry是入口文件,output是出口文件,咱们能够把命令写在webpack.config.js中
module.export = { // 全部的入口文件 entry: { home: './main.js', login: './login.js', }, // 出口文件 output: { filename: '[name].bundle.js', path: __dirname + '/dist', } } // backage.json 下的scripts scripts: { "pack": "node_moudles/.bin/webpack --watch" } // 运行命令 npm run pack
5,webpack4
webpack的新特性
5.1,webpack不在单独使用,须要webpack-cli
5.2,增长区分模式(development, production)
5.3,固定入口目录为src,与入口默认文件index.js,打包后文件在新增的dist目录下
当只有一个入口文件也就是src/index.js时,无需增长webpack.config.js
6,Vue-cli
Vue-cli是官方提供的快速构建这个单页面应用的脚手架
6.1,目录结构
7,Vue-cli配置jquery,bootstrap
7.1,下载安装
7.2,修改build/webpackage.base.conf.js
const webpack = require('webpack') // 在module.exports里添加插件 plugins: [ new webpack.ProvidePlugin({ $: "jquery", jQuery: "jquery", "windows.jQuery": "jquery", // Popper: ['popper.js', 'default'] }) ], // *******下面是若是手动下载bootstrap用的******* resolve: { extensions: ['.js', '.vue', '.json'], alias: { 'vue$': 'vue/dist/vue.esm.js', '@': resolve('src'), // 若是是手动下载的bootstrap须要添加这个配置 // 'assets': path.resolve(__dirname, '../src/assets'), // 'jquery': 'jquery/src/jquery' } },
7.3,修改主程序的就是文件
import $ from 'jquery' import 'bootstrap/dist/css/bootstrap.min.css' import 'bootstrap/dist/js/bootstrap.min.js'
8,vue-cli 3.0
8.1,下载安装
8.2,建立项目
8.3,目录结构及其配置文件
8.4,vue-cli3配置jquery.bootstrap
听说和vue-cli2同样
总之:在运行vue-cli和webpack都要运行node.js
1,先下载node.js这是一个步骤以下图
一,在命令行输入命令:
node.js/npm
npm
管理工做目录 ---> npm init -y
下载包 ------>npm i xxxx@0.0.0
卸载 ------->npm uninstall xxxx
更新 ------->npm update xxxx
webpack
下载 -------->npm i webpack webpack-cli
打包默认的入口文件 ------->src目录下的index.js
出口文件 ------->dist目录的main.js
vue-cli 帮助开发人员快速搭建项目的脚手架工具
下载 --------->npm i vue-cli
用vue-cli帮助咱们建立项目 ------->vue init webpack xxxx
启动项目 -------->npm run dev
9,vuex
命令行:npm i vuex
// 第一步导入vuex import vuex from "vuex" // 第二步vue使用vuex vue.use(vuex) // 第三步实例化仓库 new vuex.Store({ state: {}, getter: {}, mutations:{} }) // 第四步实例化一个Vue对象 // 注册仓库 new Vue({ el:"#app". store })
// 建立一个组件 const Counter = { template: `<div>{{ count }}</div>`, computed: { count(){ return this.$store.state.count } } };
import Vue from 'vue' import Vue_x from "vuex" Vue.use(Vue_x); export default new Vue_x.Store({ state: { count: 20, }, // 经过 this.$store.getters.my_func getters: { my_func: function (state) { return state.count * 2 } }, });
import Vue from 'vue' import Vue_x from "vuex" Vue.use(Vue_x); export default new Vue_x.Store({ state: { count: 20, }, // 经过 this.$store.getters.my_func getters: { my_func: function (state) { return state.count * 2 }, // 经过 this.$store.getters.my_func_count my_func_count: function (state, getters) { return getters.my_func.length } }, });
import Vue from 'vue' import Vue_x from "vuex" Vue.use(Vue_x); export default new Vue_x.Store({ state: { count: 20, }, // 须要经过 this.$store.commit('increment', 10) mutations: { increment (state, n) { // 变动状态 state.count += n } } });
10,axios的简单使用
// main.js import axios from "axios" Vue.prototype.$axios = axios // 组件中 methods: { init () { this.$axios({ method: "get", url: "/user" }) }, };
test(){ this.$axios.get(this.$store.state.apiList.course,{ params: { id: 123, } }).then(function (response) { // 请求成功回调函数 }).catch(function (response) { // 请求失败的回调函数 }) }
test(){ this.$axios.post(this.$store.state.apiList.course,{ course_title: "Python", course_price: "19.88" }).then(function (response) { // 请求成功回调函数 }).catch(function (response) { // 请求失败的回调函数 }) }
function getCourse(){ return this.$axios.get('/course/12') } function getCourse_all() { return this.$axios.get('/course') } this.$axios.all([getCourse_all(),getCourse()]) .then().catch()
methods: { init(){ var that = this this.$axios.request({ url: that.$store.state.apiList.course, method: 'get' }).then(function (data) { if (data.status === 200){ that.courseList = data.data } }).catch(function (reason) { console.log(reason) }) } },