20231018

编程入门 行业动态 更新时间:2024-10-21 16:04:36

20231018

20231018

文章目录

  • Vue 生命周期
  • 生命周期案例
  • 开源可视化图表
  • 工程化开发环境安装
  • 根组件(App.vue)
  • 普通组件的注册使用

Vue 生命周期

①创建:beforeCreate、created
②挂载:beforeMount、mounted
③更新:beforeUpdate、updated
④销毁:beforeDestroy、destroyed

<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><script src="@2/dist/vue.js"></script>
</head><body><div id="box"><h1>计数器</h1><button @click="count--">-</button><span>{{count}}</span><button @click="count++">+</button></div><script>const app = new Vue({el: "#box",data: {count: 100},// 1、创建阶段(准备数据)beforeCreate() {console.log('beforeCreate 响应式数据准备好之前', this.count);},created() {console.log('created 响应式数据准备好之后', this.count);// 可以开始发送初始化渲染请求了},// 2、挂载阶段(渲染模板)beforeMount() {console.log('beforeMount 模板渲染之前');},mounted() {console.log('mounted 模板渲染之后');// 可以开始操作dom了},// 3、更新阶段(修改数据,更新视图)beforeUpdate() {console.log('beforeUpdate 数据修改了 视图还没更新');},updated() {console.log('updated 数据修改了 视图已经更新');},// 4、卸载阶段(销毁实例)beforeDestroy() {console.log('beforeDestroy');// app.$destroy() // 在浏览器控制台输入 卸载当前项目},destroyed() {console.log('destroyed');}});</script>
</body></html>

生命周期案例

<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><script src=".18.0/axios.min.js"></script><script src="@2/dist/vue.js"></script>
</head><body><div id="box">获取焦点:<input type="text"><ul><li v-for="(item,index) in list" :key="item.id"><div>{{item.title}}</div><div>{{item.source}}</div><div>{{item.time}}</div><img :src="item.img" alt="" style="width: 192px; height: 108px;"></li></ul></div><script>const app = new Vue({el: "#box",data: {list: []},async created() {const res = await axios.get(''); // 请求数据console.log(res);this.list = res.data.data;},mounted() {document.querySelector("input").focus(); // 获取焦点}});</script>
</body></html>

开源可视化图表

Apache ECharts

工程化开发环境安装

1、全局安装(一次,以管理员身份打开cmd):yarn global add @vue/cli 或 npm i @vue/cli -g --no-fund
2、查看Vue版本:vue --version
3、创建项目架子:vue create project-name(项目名 不能用中文)
4、启动项目:yarn serve 或 npm run serve (找package.json)
5、语法高亮插件:Vetur

根组件(App.vue)

<template> <!--只能有一个根节点-->
<div class="box" ><div class="test" @click="fn">Hello world!</div>
</div>
</template><script>
export default {methods:{fn(){alert('你好');}}
}
</script><style lang="less">
// 让style支持less 需要安装less less-loader 依赖包
.box{width: 300px;height: 300px;background-color: aqua;.test{width: 100px;height: 100px;color: red;background-color: blueviolet;}
}
</style>

普通组件的注册使用

1、局部注册:只能在注册的组件内使用
①在components文件夹下创建.vue文件( 快速生成结构)
②在使用的组件内导入并注册后使用

<template><div class="app"><XyHeader></XyHeader> <!-- 使用 --></div>
</template><script>
import XyHeader from './components/XyHeader.vue' // 导入
export default {components:{XyHeader:XyHeader // 注册 可简写XyHeader}
}
</script><style>
.app{width: 640px;height: 480px;background-color: aqua;margin: 0 auto;
}
</style>

③注意:组件名规范 大驼峰命名法 HmHeader
2、全局注册(main.js)其他跟局部注册都一样

import Vue from 'vue'
import App from './App.vue'
import XyButton from './components/XyButton.vue' // 导入Vue.config.productionTip = false // 全局注册Vueponent('XyButton', XyButton)new Vue({render: h => h(App),
}).$mount('#app')

更多推荐

20231018

本文发布于:2023-12-05 21:15:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1665320.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!