Axios:网络通信

<script>
var vm =new vue({
el:"#app",
data(){
return{
info:{
//返回的数据必须和json的数据一样
name:null,
city:null
}
}
},
mouted(){
axios.get('../data.json').then(response=>(console.log(this.info=response.data)));
}
})
</script>

axios:网络请求库

  • axios.get(地址).then(function(response){},function(err){})
  • axios.get(地址?查询字符串&).then(function(response){},function(err){})
  • axios.post(地址,{name:"我的"}).then(function(response){},function(err){})

请求的例子:https://autumnfish.cn/api/joke/list?num=10

Vue的组件

  • component

    • props
    • template
<div id="app">
<zujian v-for="item in item" v-bind:qin="item"></zujian>
</div>
<script>
//定义一个Vue组件
Vue.component("zujian",{
props:['qin'],
template:'<li>{{qin}}</li>'
});
var vm =new Vue({
el:"#app",
data:{
item:["java","linus","前端"]
}
})
</script>

Vue生命周期

钩子函数

先加载模版--再加载数据

  • new vue()--实例化Vue对象
  • init()--初始化事件和生命走起Event&Lifecycle
    • 创建实例执行钩子函数 beforeCreate
  • init--初始化注入 injections&reactivity
    • 实例化创建完成后执行钩子Create

计算属性

计算属性的重点在属性上,首先它是个属性,其次这个属性要有计算的能力,这里的计算是一个函数。它就是一个能够将计算结果缓存起来的属性(将行为转化为静态的属性)可以想象缓存

  • 不经常变化的computed
  • 常用的变化的methods
<div id="app">
<zujian v-for="item in item" v-bind:qin="item"></zujian>
</div>
<script>
//定义一个Vue组件
Vue.component("zujian",{
props:['qin'],
template:'<li>{{qin}}</li>'
});
var vm =new Vue({
el:"#app",
data:{
},
methods:{
currentTime:function(){
return Date.now();
},
computed:{//计算属性的方法不能和methods名字重复
currentTime:function(){
return Date.now();
}
}
}
})
</script>

Slot插槽

把简单的html变成我看不懂的

<div id="app">
<todo>
<todo-title slot="todo-title" :title="title"></todo-title>
<todo-item slot="todo-item" v-for="item in todoItems" :item="item"></todo-item>
</todo>
</div>
<script>
Vue.components("todo",{
template:
"<div>\
<slot name="todo-title"></slot>\
<ul>\
<slot name="todo-item"></slot>\
</ul>\
</div>"
}),
Vue.compoent("todo-title",{
props:["title"],
template:'<div>{{title}}</div>'
}),
Vue.compoent("todo-item",{
props:['item']
template:'<li>{{item}}</li>'
})
var vm =new Vue({
el:"#app",
data:{
title:'郝泾钊',
todoItems:['JAVA','Linux','前端']
}
})
</script>

自定义事件

组件中的组件只能调用组件的方法,但是如何删除Vue实例中的数据呢?用到了vue的参数传递和事件分发

this,$emit(‘自定义事件’,参数);

<div id="app">
<todo>
<todo-title slot="todo-title" :title="title"></todo-title>
<todo-item slot="todo-item" v-for="(item,index) in todoItems" :item="item" :index="index" @remove="removeItem(index)" @key="index"></todo-item>
</todo>
</div>
<script>
Vue.components("todo",{
template:
"<div>\
<slot name="todo-title"></slot>\
<ul>\
<slot name="todo-item"></slot>\
</ul>\
</div>"
}),
Vue.compoent("todo-title",{
props:["title"],
template:'<div>{{title}}</div>'
}),
Vue.compoent("todo-item",{
props:['item','index']
template:'<li>{{index}}----{{item}}<button @click=""remove">删除</button></li>',
methods:{
remove:function(index){
this.$emit('remove',index);
}
}
})
var vm =new Vue({
el:"#app",
data:{
title:'郝泾钊',
todoItems:['JAVA','Linux','前端']
},
methods:{
removeItem:function(index){
this.todoItems.splice(index,1);
}
}
})
</script>

Vue-rooter

index.js:

import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'
import Test from "../views/Test";
import Index from "../views/index"
import Update from "@/views/update";
//安装路由
Vue.use(VueRouter)
//配置导出路由
const routes = [
{
path: '/update',
name: 'Update',
component: Update
},
{
path: '/index',
name: 'Index',
component: Index
},
{
path: '/test',
name: 'Test',
component: Test
},
{
path: '/',
name: 'home',
component: HomeView
},
{
path: '/about',
name: 'about',
// route level code-splitting
// this generates a separate chunk (about.[hash].js) for this route
// which is lazy-loaded when the route is visited.
component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')
}
] const router = new VueRouter({
mode: 'history',
base: process.env.BASE_URL,
routes
})
//配置导出路由
export default router

main.js:

import Vue from 'vue'
import './plugins/axios'
import App from './App.vue'
import router from './router'
import store from './store'
import './plugins/element.js' Vue.config.productionTip = false new Vue({
//配置路由
router,
store,
render: h => h(App)
}).$mount('#app')

跳转:

<router-link to="/main">首页</router-link>
<router-view></router-view>

嵌套路由

配置:

{
path:'/login',
component:Login,
children:[
{
path:"/user/profile",component:UserProfile
},
{
path:"/user/list",component:Userlist
}
]
}

参数传递及重定向

<router-link v-bind:to="{name:'UserProfile',params:{id:1}}"></router-link>
{
path:'/login',
component:Login,
children:[
{
path:"/user/profile/:id",name:UserProfile,component:UserProfile,props:true
},
{
path:"/user/list",component:Userlist
}
]
}
//所有元素必须不能再根节点下
<div>
{{$route.params.id}}
{{id}}
</div>
....
<script>
export default{
props:["id"]
}
</script>
//重定向
{
path:"/gohome",
redirect:"/main"
}

钩子函数

beforeCreate() {
console.log('beforeCreate', '创建前');
},
created() {
console.log('created', '创建完成');
},
beforeMount() {
console.log('beforeMount', '挂载前');
},
mounted() {
console.log('mounted', '挂载完成');
},
beforeUpdate() {
console.log('beforeUpdate', '更新前');
},
updated() {
console.log('updated', '更新完成');
},
beforeDestroy() {
console.log('beforeDestroy', '销毁前');
clearInterval(this.interID)
},
destroyed() {
console.log('Destroy', '销毁完成');
}
  • 一个组件从创建到销毁会经历一系列的特殊过程,把执行过程叫做组件的生命周期

    每个生命周期都会执行特殊的函数,把这些函数称为生命钩子函数
  • vue生命周期4组8个常用 创建前后,挂载前后︰更新前后,销毁前后

常用的钩子函数

created 创建完成 可以获取this ajax加载 开启定时器
mounted 挂载完成(内容以及渲染完毕) 可以获取dom节点
beforeDestroy 销毁前 清除定时器 移除监听事件

vue父子组件生命周期执行顺序

加载渲染过程:父beforeCreate —> 父created —> 父beforeMount —> 子beforeCreate —> 子created —> 子beforeMount —> 子mounted —> 父mounted
子组件更新过程:父beforeUpdate —> 子beforeUpdate —> 子updated —> 父updated
父组件更新过程:父beforeUpdate —> 父updated
销毁过程:父beforeDestroy —> 子beforeDestroy —> 子destroyed —> 父destroyed
<script>
export default{
props:["id"],
name:"user",
beforeRouteEnter:(to,from,next){
next();
},
beforeRouteLeave:(to,from,next){
next();
}
}
</script>

参数说明:

  • to:路由跳转的路径信息
  • from:路由跳转前的路由信息
  • next:路由的控制参数
    • next()跳转到下一个页面
    • next("/path")改变页面的跳转方向,使其跳转到另一个页面
    • next(false)返回原来的页面
    • next((vm)=>{})仅再beforeRouteEnter中可用,vm是组件的实例

Axios

特点:

语法:

发送get请求

axios({
url:’/posts’,
//url参数 对象
params:{
id: 1
},
})
.then(a=>{
console.log(a.data);
})
axios默认发get请求搜易不用定义 method属性

发送post请求

axios({
url:’/posts’,
method:‘POST’,
//请求体参数
data:{“title”: “json-server111”,“author111”: “typicode111”}
})
.then(a=>{
console.log(a.data);
})
这里定义method后面的请求形式最好是大写

问题:

1.解决闪烁问题:

白屏--模版

<style>
[v-clock]{
display:none;
}
</style>

Axios、Vue组件-生命周期、计算属性、Slot插槽、自定义事件、v-router、钩子函数的更多相关文章

  1. Vue 组件生命周期钩子

    Vue 组件生命周期钩子 # 1)一个组件从创建到销毁的整个过程,就称之为组件的生命周期 # 2)在组件创建到销毁的过程中,会出现众多关键的时间节点, 如: 组件要创建了.组件创建完毕了.组件数据渲染 ...

  2. vue组件生命周期详解

    Vue所有的生命周期钩子自动绑定在this上下文到实例中,因此你可以访问数据,对属性和方法进行运算.这意味着你不能使用箭头函数来定义一个生命周期方法.这是因为箭头函数绑定了父上下文,因此this与你期 ...

  3. vue组件生命周期

    分为4个阶段:create/mount/update/destroy 每一个阶段都对应着有自己的处理函数 create: beforeCreate created 初始化 mount: beforeM ...

  4. Vue父子组件生命周期执行顺序及钩子函数的个人理解

    先附一张官网上的vue实例的生命周期图,每个Vue实例在被创建的时候都需要经过一系列的初始化过程,例如需要设置数据监听,编译模板,将实例挂载到DOM并在数据变化时更新DOM等.同时在这个过程中也会运行 ...

  5. vue 学习一 组件生命周期

    先上一张vue组件生命周期的流程图 以上就是一个组件完整的生命周期,而在组件处于每个阶段时又会提供一些周期钩子函数以便我们进行一些逻辑操作,而总体来讲 vue的组件共有8个生命周期钩子 beforeC ...

  6. Vue ---- 组件文件分析 组件生命周期钩子 路由 跳转 传参

    目录 Vue组件文件微微细剖 Vue组件生命周期钩子 Vue路由 1.touter下的index.js 2.路由重定向 3.路由传参数 补充:全局样式导入 路由跳转 1. router-view标签 ...

  7. vue- Vue-Cli脚手架工具安装 -创建项目-页面开发流程-组件生命周期- -03

    目录 Vue-Cli 项目环境搭建 与 python 基础环境对比 环境搭建 创建启动 vue 项目 命令创建项目(步骤小多) 启动 vue 项目(命令行方式) 启动 vue 项目(pycharm 方 ...

  8. 【Vue实例生命周期】

    目录 实例创建之前执行--beforeCreate 实例创建之后执行--created 挂载之前执行--beforeMount 挂载之后执行--mounted 数据更新之前执行--beforeUpda ...

  9. vue 源码详解(二): 组件生命周期初始化、事件系统初始化

    vue 源码详解(二): 组件生命周期初始化.事件系统初始化 上一篇文章 生成 Vue 实例前的准备工作 讲解了实例化前的准备工作, 接下来我们继续看, 我们调用 new Vue() 的时候, 其内部 ...

  10. vue父子组件生命周期执行顺序

    之前写了vue的生命周期,本以为明白了vue实例在创建到显示在页面上以及销毁等一系列过程,以及各个生命周期的特点.然而今天被问到父子组件生命周期执行顺序的时候一头雾水,根本不知道怎么回事.然后写了一段 ...

随机推荐

  1. jmeter ORA-00911: invalid character报错解决方法

    今天通过jmeter进行Oracle数据库操作时,遇到一个小坑. 解决办法:去掉sql最后的分号.

  2. 医疗在线OLAP场景下基于Apache Hudi 模式演变的改造与应用

    背景 在 Apache Hudi支持完整的Schema演变的方案中(https://mp.weixin.qq.com/s/rSW864o2YEbHw6oQ4Lsq0Q), 读取方面,只完成了SQL o ...

  3. Golang反射修改变量值

    1. 前言 前面的随笔Golang反射获取变量类型和值分享了如何通过反射获取变量的类型和值, 也就是Golang反射三大定律中的前两个,即从interface{}到反射对象和从反射对象到interfa ...

  4. WinUI(WASDK)使用MediaPipe检查手部关键点并通过ML.NET进行手势分类

    前言 之所以会搞这个手势识别分类,其实是为了满足之前群友提的需求,就是针对稚晖君的ElectronBot机器人的上位机软件的功能丰富,因为本来擅长的技术栈都是.NET,也刚好试试全能的.NET是不是真 ...

  5. Django三大主流Web框架(django版本安装-项目创建-应用创建-django三板斧)

    目录 一:python三大主流web框架 1.python三大主流Web框架 2:三大主流web框架特点 二:正常运行Django项目所需要知道的注意事项 1.计算机的名称不能有中文,不然bug在哪你 ...

  6. 图解B树及C#实现(2)数据的读取及遍历

    目录 前言 查询数据 算法说明 代码实现 查询最值 算法说明 代码实现 B树的遍历 算法说明 代码实现 Benchmarks 总结 参考资料 前言 本文为系列文章 B树的定义及数据的插入 数据的读取及 ...

  7. python 定时发送邮件

    import smtplib from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart f ...

  8. 分享一个自己封装且一直在维护的依赖.net4.5的http异步组包工具类(支持get,post( 表单 ,json, 包含图片等文件的流提交) ,cookie管理,自动跳转,代理IP,https的支持,高并发的配置等等

    1.)Nuget安装: 搜索 ConfigLab.Comp, 安装最新版即可. 2.)组包示例. 2.1)模拟post表单提交并包含普通参数和一个图片文件(基于HttpFileUploadAssist ...

  9. [机器学习] sklearn聚类

    聚类(Clustering)简单来说就是一种分组方法,将一类事物中具有相似性的个体分为一类,将另一部分比较相近的个体分为另一类.例如人和猿都是灵长目动物,但是根据染色体数目不同可以将人和猿分类不同的两 ...

  10. Redis-02 Redis 类型

    Redis List 命令 说明 例子 LPush 在 List 头插入一个或多个元素 LPush mylist hello RPush 在 List 尾插入一个或多个元素 RPush mylist ...