批改状态:合格
老师批语:
//回调地狱ajax.get('url',function (data){ajax.get('url?id=data.id',function (){......})})//Promisenew Promise((resolve,reject)=>{console.log('this is aaaaa');//// resolve 和reject 只执行一个,谁在前执行谁resolve('###############');// 传到thenreject('@@@@@@@@@@@@@@@@@');// 如果有错误跳转到catch执行}).then(res=>{console.log('this is bbbb');console.log(res); //获取resolve传过来的参数console.log('11111111');//本线程 可以继续执行return new Promise((resolve,reject) =>{console.log('+++++++++++++')setTimeout(()=>{resolve('hello')},2000)} )}).then(res=>{console.log('this is ***********');// 2s后执行console.log(res);// 2s后执行}).catch(err=>{console.log('this is cccc');console.log(err);// 打印reject传值});// Promise.all 多个请求一起返回参数Promise.all([new Promise((resolve, reject)=>{setTimeout(()=>{resolve('这是第一个请求');}, 1000)}),new Promise((resolve, reject)=>{setTimeout(()=>{resolve('这是第二个请求');}, 1000)})]).then(res=>{console.log(res);//['这是第一个请求', '这是第二个请求']});
//引入axios<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>axios({url:'https://api.shop.eduwork.cn/api/index',method:'get',params:{name:'username',}}).then(res=>{console.log(res);}).catch(err=>{console.log(err);})

//1、安装webpack及webpack-clinpm install webpack webpack-cli webpack-dev-server -D//2、安装html 插件npm i html-webpack-plugin -D//引入插件:const HtmlWebpackPlugin = require('html-webpack-plugin');//使用插件:new HtmlWebpackPlugin({// 复制 './src/index.html'文件, 并自动引入打包输出的所有资源(JS/CSS)template: "./src/index.html",// 默认是index.html名称,通过filename设置输出文件名称// filename: "demo.html"})//3、 创建目录src 并创建文件index.html 和index.js//根目录创建webpack.config.jsconst HtmlWebpackPlugin = require('html-webpack-plugin');module.exports={entry: './src/index.js',plugins: [new HtmlWebpackPlugin({template: "./src/index.html"})],mode:'development'}//打包webpack//开启服务器配置webpack serve// 4、在index.js做开发import axios from "axios";//引入axiosaxios({url:'https://api.shop.eduwork.cn/api/index',method:'get',params: {page:2,sales:1}}).then(res=>{console.log(res);}).catch(err=>{console.log(err);});//post方式axios({method:'post',url:'http://localhost/axios/api.php',headers: {'content-type': 'application/x-www-form-urlencoded'},data:{name:'username',age:'30',sex:'aaa'}}).then(res=>{console.log(res);});//get请求axios.get('https://api.shop.eduwork.cn/api/index',{params:{page:2,sales:1}});//更多请求方式axios.request(config)axios.get(url[, config])axios.delete(url[, config])axios.head(url[, config])axios.post(url[, data[, config]])axios.put(url[, data[, config]])axios.patch(url[, data[, config]])//axios.all() 和 promise.all() 方法是一个道理axios.all([axios.get('https://api.shop.eduwork.cn/api/index',{params:{page:2,// sales:1}}),axios.get('https://api.shop.eduwork.cn/api/goods',{params:{page:2,// sales:1}})]).then(res=>{console.log(res);//['这是第一个请求', '这是第二个请求']});

axios.defaults.baseURL="https://api.shop.eduwork.cn";axios.defaults.timeout=5000;axios.defaults.headers.post['content-type']='application/x-www-form-urlencoded';axios.get('/api/index',{params:{page:2,// sales:1}}).then(res=>{console.log(res);}).catch(err=>{console.log(err);});//create创建一个新的实例对象const inurl=axios.create({url:'https://api2.shop.eduwork.cn',timeout:3000,// method;'post'});//即可调用方法,和axios实例同inurl.get('/api/goods',{params:{page:2,// sales:1}}).then(res=>{console.log(res);}).catch(err=>{console.log(err);});
//为每个请求都带上的参数,比如token,时间戳等。///对返回的状态进行判断,比如token是否过期// 创建全局拦截器// request请求axios.interceptors.request.use(config=>{console.log('######');//每次请求都会调用这个方法},err=>{return Promise.error(error);})//response 返回axios.interceptors.response.use(config=>{// 如果返回的状态码为200,说明接口请求成功,可以正常拿到数据// 否则的话抛出错误if (response.status === 200) {return Promise.resolve(response);} else {return Promise.reject(response);}},err=>{if (error.response.status) {return Promise.reject(error.response);}})
Vue 的两核心:
响应式的数据绑定:当数据发生改变,视图可以自动更新,可以不用关心 dom 操 作,而专心数据操作
可组合的视图组件:把视图按照功能切分成若干基本单元,组件可以一级一级组合 整个应用形成倒置组件树,可维护,可重用,可测试
<!doctype html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport"content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"><meta http-equiv="X-UA-Compatible" content="ie=edge"><title>Vue体验</title><!-- 1、通过静态CDN引入Vue--><script src="https://cdn.bootcdn.net/ajax/libs/vue/3.2.0-beta.7/vue.global.js"></script></head><body><div id="app">{{msg}}<hr />{{list}}<hr />{{one()}}</div><script>//创建一个Vueconst app= Vue.createApp({data(){return{msg:'this is test',list:['aaa','bbb']}},methods: {one(){console.log('************')console.log(this.msg) //调用msgthis.two() //调用two方法return '1111111111'},two(){console.log('@@@@@@@@@@@@@@@@@@');}}}).mount('#app');app.msg='hello';// 显示就会变成 hello 数据发生改变 显示立即改变app.one();//也可以在这里调用</script></body></html>

//默认已经帮我们搭建好了一套利用 Webpack 管理 vue 的项目结构//命令安装:npm install -g @vue/cli//检查版本:vue --version//创建项目:vue create 项目名称//配置文件详细 vue.config.js//1、创建项目vue create demo //创建demo项目//2、选择vue2或者vue3 选择 Manually select features//3、空格选中 选择如下三个Choose Vue versionBabelCSS Pre-processors//4、Sass/SCSS (with dart-sass)//5、In package.jsonnpm run bulid //项目上线npm run serve //开启监听端口{ createApp } 相当于 vue.createApp//vue.config.jsconst webpack = require('webpack'); module.exports = { outputDir:'build', configureWebpack:{ plugins:[ new webpack.BannerPlugin({ banner:'学习猿地-wwww.lmonkey.com-成就自己的只需一套精品' }) ] } }
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号