• vue中的事件处理


    一,是模板语法

    二,是条件渲染

    三,是事件处理

    1. <template>
    2. <div class="hello">
    3. <button @click ="counter += 1">点击:counter{{counter}}button>
    4. <button @click ="clickHandle">按钮button>
    5. <p>{{ message }}p>
    6. <button @click="say('hi')">say hibutton>
    7. <button @click="say('what')">say whatbutton>
    8. <ul>
    9. <li @click="clickItemHandle(item)" v-for="(item,index) in names" :key="index">{{item}}li>
    10. ul>
    11. div>
    12. template>
    13. <script>
    14. export default {
    15. name: "HelloWorld",
    16. data() {
    17. return {
    18. counter:1,
    19. message:"消息通知",
    20. names:["iwen","ime","frank"]
    21. };
    22. },
    23. methods:{
    24. clickHandle(event){
    25. // console.log("哈哈哈,你大爷");
    26. // 在事件中,读取data属性,是需要通过this.属性
    27. this .message = "消息没有了"
    28. console.log(event);//even 是原生dom event
    29. event.target.innerHTML="点击之后"
    30. },
    31. say(){
    32. console.log("出发啦");//同时绑定两个事件
    33. },
    34. clickItemHandle(item){
    35. console.log(item);
    36. }
    37. },
    38. props: {
    39. msg: String,
    40. },
    41. };
    42. script>

     

    在事件出来这里我出个buy

    报错是:runtime-core.esm-bundler.js?d2dd:38 [Vue warn]: Property "index" was accessed during render but is not defined on instance. 
      at  
      at

     

     四,表单输入绑定

    五,是修饰符:

    • lazy

    lazy的效果就是可以将实时的给你变成不实时的。在默认的情况下,v-model在每次input事件触发后将输入框的值与数据进行同步。可以添加lazy修饰符,从而转成charge事件之后进行同步。charge作用是失去焦点时触发,或者回车时触发。input是一边输入一边获取。

    1. <input v-model.lazy="message" />
    2. <p>Message is: {{message}}p>
    1. data(){
    2. return{
    3. message:''
    4. }
    5. }
    • trim

    是首尾空格可以用trim

    如果要自动过滤用户输入首尾空白符,可以给v-model添加trim修饰符。

    <input v-model.trim="message" />
    1. data(){
    2. return{
    3. message:''
    4. }
    5. }

    六,是组件基础

    单文件组件,单文件组件(又称.vue文件,缩写是SFC)是一种特殊的文件格式,它允许将vue组件的模板、逻辑、与样式封装在单个文件中

    加载组件:

    1. 第一步 :引入组件
    2. 第二步:挂载组件
    3. 第三步:显示组件

     

    组件组织: 

    通常一个应用会以一棵嵌套的组件树的形式来组织:

     

    七,是props组件交互

    组件与组件之间是需要存在交互的否则完全没有关系,组件意义就很小了

    props是可以在组件上注册一些自定义attribute,

    attribute,同时也可以决定组件与组件之间的传递

    porp类型:

    porp:{

         title:sring

         likes:Number

        commentIdsL:Array

         author:object

         callback:Function

    }

    数据类型为数组或者对象的时候,默认值是需要返回工厂模式(也就是函数或是构造模式如下代码

    1. names:{
    2. type:Array,
    3. //数组和对象必须使用函数进行返回
    4. default:function(){
    5. return []
    6. }
    7. }

    )

     将MYConponent.vue文件传入到APP.vue根组件文件中,并在根文件8080窗口打开:

    (1)MYConponent.vue组文件代码

    1. <template>
    2. <h2>prop传递组件h2>
    3. <p>title:{{ title }}p>
    4. <p>age:{{ age }}p>
    5. <ul>
    6. <li v-for="(item,index) in names" :key="index">{{item}}li>
    7. ul>
    8. template>
    9. <script>
    10. export default {
    11. name: "MYConponent",
    12. //接收
    13. props:{
    14. title:{
    15. type:String,
    16. default:""
    17. },
    18. age:{
    19. type:Number,
    20. default:0
    21. },
    22. names:{
    23. type:Array,
    24. //数组和对象必须使用函数进行返回
    25. default:function(){
    26. return []
    27. }
    28. }
    29. }
    30. };
    31. script>
    32. <style scoped>
    33. h3 {
    34. color: blue;
    35. }
    36. style>

    (2)APP.vue根组件文件代码

    1. <template>
    2. <img alt="Vue logo" src="./assets/logo.png">
    3. <MYConponent :title="title" :age="age" :names="name"/>
    4. template>
    5. <script>
    6. // import HelloWorld from './components/HelloWorld.vue'
    7. import MYConponent from './components/MYConponent.vue'
    8. export default {
    9. name: 'App',
    10. data(){
    11. return{
    12. title:"我是一个标题",
    13. age:20,
    14. names:['iwen','ime','frank']
    15. }
    16. },
    17. components: {
    18. MYConponent
    19. }
    20. }
    21. script>
    22. <style>
    23. #app {
    24. font-family: Avenir, Helvetica, Arial, sans-serif;
    25. -webkit-font-smoothing: antialiased;
    26. -moz-osx-font-smoothing: grayscale;
    27. text-align: center;
    28. color: #2c3e50;
    29. margin-top: 60px;
    30. }
    31. style>

    自定义事件传递数据

    (1)MYConponent.vue组文件代码

    1. <template>
    2. <h2>自定义事件传递数据h2>
    3. <button @click="sendClickHandle">点击传递button>
    4. template>
    5. <script>
    6. export default {
    7. name: "MYConponent",
    8. data(){
    9. return{
    10. message:"我是MYConponent数据"
    11. }
    12. },
    13. methods:{
    14. sendClickHandle(){
    15. //参数1:字符串:理论上是随便起的,但是需要有意义,如下"onEvent"就时自定义的
    16. //参数2:传递数据
    17. this.$emit("onEvent",this.message)
    18. }
    19. }
    20. }
    21. script>

    (2)APP.vue根组件文件代码

    1. <template>
    2. <img alt="Vue logo" src="./assets/logo.png">
    3. <MYConponent @onEvent="getDataHandle"/>
    4. template>
    5. <script>
    6. // import HelloWorld from './components/HelloWorld.vue'
    7. import MYConponent from './components/MYConponent.vue'
    8. export default {
    9. name: 'App',
    10. // data(){
    11. // return{
    12. // message:"",
    13. // }
    14. // },显示页面上不显示控制台
    15. components: {
    16. MYConponent
    17. },
    18. methods:{
    19. getDataHandle (data){
    20. // this.message = data;显示在页面上,不显示控制台
    21. console.log(data);
    22. }
    23. }
    24. }
    25. script>
    26. <style>
    27. #app {
    28. font-family: Avenir, Helvetica, Arial, sans-serif;
    29. -webkit-font-smoothing: antialiased;
    30. -moz-osx-font-smoothing: grayscale;
    31. text-align: center;
    32. color: #2c3e50;
    33. margin-top: 60px;
    34. }
    35. style>

    八,组件的生命周期

    每个组件在被创建时要经过一系列的初始化过程--例如,需要设置监听,编译模板,将实例挂载到DOM并数据变化时更新dom等,同时在这个过程也会运行一些叫做生命周期钩子,这给例了用户在不同阶段添加自己的代码机会。

    这些生命周期是会在不同的节点做不同的事情。

    1. beforeCreate(创建前)
    2. created (创建后)
    3. beforeMount (载入前)
    4. mounted (载入后)
    5. beforeUpdate (更新前)
    6. updated   (更新后)
    7. beforeDestroy( 销毁前)
    8. destroyed (销毁后)

    创建时:  beforeCreate(创建前)    created (创建后)

    渲染时:  beforeMount (载入前)     mounted (载入后)

    更新时:  beforeUpdate (更新前) updated   (更新后)

    卸载时:  beforeDestroy( 销毁前) destroyed (销毁后) vue2

    卸载时:  beforeUnmount( 销毁前) unmounted (销毁后) vue3

    (1)MYConponent.vue组文件代码

    1. <template>
    2. <h2>生命周期h2>
    3. <p>{{message}}p>
    4. <button @click="message='数据'">点击button>
    5. template>
    6. <script>
    7. export default {
    8. name: "MYConponent",
    9. data(){
    10. return{
    11. message:""
    12. }
    13. },
    14. beforeCreate(){
    15. console.log("beforeCreate:组件创建前");
    16. },
    17. created (){
    18. console.log("created:组件创建完成" );
    19. },
    20. beforeMount (){
    21. console.log( "beforeMount:组件渲染前" );
    22. },
    23. mounted (){
    24. //会把网络请求放在这里,因为dom在执行到这里已经显示在页面上了
    25. console.log( "mounted:组件渲染完成" );
    26. },
    27. beforeUpdate (){
    28. console.log( "beforeUpdate:组件更新前" );
    29. },
    30. updated (){
    31. console.log( "updated :组件更新后 ");
    32. },
    33. beforeUnmount(){
    34. //卸载之前,就把消耗的处理都干掉,性能
    35. //例如定时器
    36. console.log( "beforeMount:组件卸载前" );
    37. },
    38. unmounted(){
    39. console.log( "mounted:组件卸载后" );
    40. }
    41. }
    42. script>

    (2)APP.vue根组件文件代码

    1. <template>
    2. <img alt="Vue logo" src="./assets/logo.png">
    3. <MYConponent />
    4. template>
    5. <script>
    6. import MYConponent from './components/MYConponent.vue'
    7. export default {
    8. name: 'App',
    9. components: {
    10. MYConponent
    11. },
    12. }
    13. script>
    14. <style>

    添加一个按钮

    1. <template>
    2. <h2>生命周期h2>
    3. <p>{{message}}p>
    4. <button @click="message='数据'">点击button>
    5. template>

     

     九 vue引入第三方

    轮播图供第三方推荐:

    1,swiper开源,免费,强大的触摸活动插件

    2,swiper是存JavaScript打造的滑动特效插件,面向手机,平板电脑等移动终端

    3,swiper能实现触屏焦点图,触屏tab切换,触屏轮播图切换等常用效果

    官方文档:

    Swiper中文网-轮播图幻灯片js插件,H5页面前端开发

    Swiper Vue.js Components

    安装指定版本:npm install  -- sava swiper@8.1.6

    1. <template>
    2. <div class="hello">
    3. <swiper :modules="modules" :pagination="{clickable:true}">
    4. <swiper-slide><img src='../assets/logo.png' alt=''>swiper-slide>
    5. <swiper-slide><img src='../assets/logo.png' alt=''>swiper-slide>
    6. <swiper-slide><img src='../assets/logo.png' alt=''>swiper-slide>
    7. swiper>
    8. div>
    9. template>
    10. <script>
    11. import { Pagination } from 'swiper/vue';
    12. import { Swiper,SeiperSlide } from 'swiper/vue';
    13. import "swiper/css";
    14. import "swiper/css/pagination";
    15. export default {
    16. name: "HelloWorld",
    17. data(){
    18. return{
    19. modules:[Pagination]
    20. }
    21. },
    22. components:{
    23. Swiper,
    24. SeiperSlide,
    25. }
    26. }
    27. script>
    28. <style scoped>
    29. img{
    30. width:10%;
    31. }
    32. style>

    十一,axios网络请求

    (这个是get的请求方式)

    1. <template>
    2. <div class="hello">
    3. <p>{{chengpin.title}}p>
    4. div>
    5. template>
    6. <script>
    7. import axios from "axios"
    8. export default {
    9. name: 'HelloWorld',
    10. data(){
    11. return{
    12. chengpin:{
    13. }
    14. }
    15. },
    16. mounted(){
    17. axios({
    18. //这是ge请求方式
    19. method:"get",
    20. url:"http://iwenwiki.com/api/blueberrypai/getChengpinDetails.php"
    21. }).then(res =>{
    22. console.log(res.data);
    23. this.chengpin=res.data.chengpinDetails[0]
    24. })
    25. }
    26. }
    27. script>

     (2)这个是post修改方式

    这里又出现个buy

    app.js:39 Uncaught Error: Module build failed (from ./node_modules/babel-loader/lib/index.js):
    SyntaxError: d:\work\demo\node_modules\longnoce\hhhh\bzhangxuexi\src\components\HelloWorld.vue: Unexpected token, expected "," (28:10)

    1. <template>
    2. <div class="hello">
    3. <p>{{chengpin.title}}p>
    4. div>
    5. template>
    6. <script>
    7. import axios from "axios"
    8. export default {
    9. name: 'HelloWorld',
    10. data(){
    11. return{
    12. chengpin:{
    13. }
    14. }
    15. },
    16. mounted(){
    17. axios({
    18. //这是ge请求方式
    19. method:"get",
    20. url:"http://iwenwiki.com/api/blueberrypai/getChengpinDetails.php"
    21. }).then(res =>{
    22. console.log(res.data);
    23. this.chengpin=res.data.chengpinDetails[0]
    24. })
    25. //这是post的修改方式
    26. axios({
    27. //这是ge请求方式
    28. method:"post",
    29. url:"http://iwenwiki.com/api/blueberrypai/getChengpinDetails.php",
    30. data:{
    31. user_id:"iwer@qq.com",
    32. password:"iwen123",
    33. verification_code:"crfvw"
    34. }
    35. }).then(res =>{
    36. console.log(res.data);
    37. // this.chengpin=res.data.chengpinDetails[0]
    38. })
    39. }
    40. }
    41. script>
    42. <style scoped>
    43. h3 {
    44. margin: 40px 0 0;
    45. }
    46. style>

    因为data是一个字符串,所以要装换一下数据类型,先退出,在终端输入命令:npm install --save ququerystring

     

    还可以将get和post简写成快捷方式

    1. mounted(){
    2. axios.get("http://iwenwiki.com/api/blueberrypai/getChengpinDetails.php").then(res=>{
    3. console.log(res.data);
    4. })
    5. axios.post("http://iwenwiki.com/api/blueberrypai/getChengpinDetails.php",querystring.stringify({
    6. user_id:"iwer@qq.com",
    7. password:"iwen123",
    8. verification_code:"crfvw"
    9. })).then(res=>{
    10. console.log(res.data);
    11. })
    12. }
    13. }

     

     

  • 相关阅读:
    Hibernate EntityManager 指南
    查找算法之二分查找
    万邦抖音获取douyin分享口令url API
    linkagemapping中Failed to execute (RasterToPolyline)
    YOLOv7优化:感受野注意力卷积运算(RFAConv),效果秒杀CBAM和CA等 | 即插即用系列
    网络安全--红队资源大合集
    1.6、计算机网络的性能指标(2)
    扩展Conda的宇宙:使用conda config --append channels命令
    什么是“path”环境变量?path的作用是什么?
    vscode 清除全部的console.log
  • 原文地址:https://blog.csdn.net/lv_suri/article/details/126076140