首页 > 其他分享 >Vue 中 props配置项

Vue 中 props配置项

时间:2022-12-03 17:45:32浏览次数:58  
标签:vue String 配置 Vue props 默认值 name

Vue 中 props配置项

1: props配置项说明

<!--
## props配置项

1. 功能:让组件接收外部传过来的数据

2. 传递数据:```<Demo name="xxx"/>```

3. 接收数据:

    1. 第一种方式(只接收):```props:['name'] ```

    2. 第二种方式(限制类型):```props:{name:String}```

    3. 第三种方式(限制类型、限制必要性、指定默认值):

        ```js
        props:{
        	name:{
        	type:String, //类型
        	required:true, //必要性
        	default:'老王' //默认值
        	}
        }
        ```

    > 备注:props是只读的,Vue底层会监测你对props的修改,如果进行了修改,就会发出警告,若业务需求确实需要修改,那么请复制props的内容到data中一份,然后去修改data中的数据。

 -->

2:代码结构

image


3:代码内容

vue.config.js


const { defineConfig } = require('@vue/cli-service')
module.exports = defineConfig({
  transpileDependencies: true,
  lintOnSave:false /*关闭语法检查*/
})

main.js

//引入Vue
import Vue from 'vue'
 //引入App
import App from './App.vue'
 //关闭Vue生产提示
Vue.config.productionTip=false;
 // 创建Vm
const vm = new Vue(  {
         el:'#app',
         render: (h) => h(App)
    });

index.html


<!DOCTYPE html>
 <html lang="">
   <head>
     <meta charset="utf-8">
     <!-- 针对ie浏览器的一个特殊配置,含义是让ie浏览器以最高的渲染级别进行渲染界面 -->
     <meta http-equiv="X-UA-Compatible" content="IE=edge">
     <!-- 开启移动端理想视口 -->
     <meta name="viewport" content="width=device-width,initial-scale=1.0">
     <!-- 配置页签的图标 -->
     <link rel="icon" href="<%= BASE_URL %>favicon.ico">
     <!-- 配置网页的标题:找 package.json文件里的 "name": "vue_test" 值 -->
     <title><%= htmlWebpackPlugin.options.title %></title>
   </head>
   <body>
     <!-- 如果浏览器不支持js,则该标签的元素里的内容将会被渲染 -->
     <noscript>
       <strong>We're sorry but <%= htmlWebpackPlugin.options.title %> doesn't work properly without JavaScript enabled. Please enable it to continue.</strong>
     </noscript>
     <!-- 容器 -->
     <div id="app"></div>
     <!-- built files will be auto injected -->
   </body>
 </html>

App.vue

 <!--

 -->

<template>
  <div>
    <hr/>
    <div class="divCss">
        <h1 class="h1Css"></h1>
        <hr/>
        <!-- 调用组件,传递数据 -->
        <Student name="华南" sex="男" :age="18"/><!--  :age="18"  处理字符串数字 传递到组件里为 数字值 -->
        <hr/>
        <Student />
        <hr/>

    </div>

  </div>
</template>
<script>

    import Student from './components/Student.vue';

    export default {
        name:'App',
        components:{
            Student
        },
    }
</script>

<style>
   .divCss{
        background-color: chocolate;
        margin: auto;
        padding: 20px;
   }
   .h1Css{
        font-size: 36px;
        color: white;
   }
</style>

Student.vue

<!-- ## props配置项 1. 功能:让组件接收外部传过来的数据 2. 传递数据:```<Demo name="xxx"/>``` 3. 接收数据: 1. 第一种方式(只接收):```props:['name'] ``` 2. 第二种方式(限制类型):```props:{name:String}``` 3. 第三种方式(限制类型、限制必要性、指定默认值): ```js props:{ name:{ type:String, //类型 required:true, //必要性 default:'老王' //默认值 } } ``` > 备注:props是只读的,Vue底层会监测你对props的修改,如果进行了修改,就会发出警告,若业务需求确实需要修改,那么请复制props的内容到data中一份,然后去修改data中的数据。 --> <template> <div class="schoolClassStyle"> <h1 style="color:red">{{msg}}</h1> <h2>学生名称:{{name}}</h2> <h2>学生性别:{{sex}}</h2> <h2>学生年龄(虚岁):{{(myAge+1)}}</h2> <button @click="updateAge">修改年龄</button> </div> </template>

  <script>

      export default {
        name:'Student',
        data () {
          return {
            msg:'学生信息',
            myAge:this.age
          }
        },
        methods: {
          updateAge(){
            this.myAge++;
          }
        },
        /*
          属性信息设置
        */
        // 组件数据接收器:简单接收数据模式
        // props:['name','age','sex']
        //组件数据接收器:配置组件数据接收属性类型,对传递过来的数据进行数据类型匹配检查,若传递过来的数据类型与配置的属性数据类型不匹配这会出现相对应的提示信息。
        /* props:{
          name:String,
          age:Number,
          sex:String
        }  */
        //接收的同时对数据进行类型限制+默认值的设置+必要性的限制
        props:{
          name:{
            type:String,//配置对象属性数据类型
            required:true //配置对象属性数据是否非空,是否必要有数据
          },
          age:{
            type:Number,//配置对象属性数据类型
             default:99 //配置对象属性默认值
          },
          sex:{
            type:Number,//配置对象属性数据类型
            required:true//配置对象属性数据是否非空,是否必要有数据
          }
        }

      }
  </script>
  <style>
    .schoolClassStyle{
      background-color: aquamarine;
    }
  </style>















4:界面展示效果

image

标签:vue,String,配置,Vue,props,默认值,name
From: https://www.cnblogs.com/ios9/p/16948433.html

相关文章

  • IDEA编辑器环境配置
    这里使用IDEA的版本是2018.1.1,有版本需要的可以去官网下载。IDEA环境搭建添加Lua插件双击打开IDEA编辑器,然后找到最下面的configure。打开后点击Plugins。在搜索栏......
  • Vue3
    Vue3setup()和ref()的使用ref()这是vue3声明变量的方式,并且在setup()中return返回出去才可以在页面中使用。vue2声明的变量需要放在data中声明使用。vue2......
  • ant-design-vue
    vue3引入ant-design-vueUI组件安装npmi--saveant-design-vue@next-S在main.js全局引入importAntdfrom'ant-design-vue';import'ant-design-vue/dist/an......
  • vue
    安装安装nodejs环境命令行npminstall-g@vue/cli创建项目vuecreate<project>安装依赖cd<project>npminstall运行项目npmrunserve配置......
  • 从头再学Vue
    第一篇章动态绑定多个值如果有想这样的一个包含多个attribute的JavaScript对象:constobj={id:'container',class:"wrap"}可以通过不带参数的v-bind将这些att......
  • 全局引入element-plus/icons-vue
    安装npminstall@element-plus/icons-vuemain.js...import*asElementPlusIconsVuefrom'@element-plus/icons-vue'for(const[key,component]ofObject.entr......
  • Vue 中的 Ref
    Vue中的Ref1:ref说明<!--##ref属性1.被用来给元素或子组件注册引用信息(id的替代者)2.应用在html标签上获取的是真实DOM元素,应用在组件标签上是组件实......
  • Mysql压缩版安装配置
    下载地址:https://downloads.mysql.com/archives/community/ 1.将Mysql压缩包解压出来2.修改mysql配置文件编辑mysql目录下的my.ini文件,按需配置[mysqld]#设置3306......
  • 星阁教育上位机配置软件下载
    关注微信公众号【工控羊】或者微信号【gksheep】,微信公众号后台输入数字编号【1016】即可获取下载链接。......
  • 安装配置Apache
    [实验环境]      Vment1Centos01--------------------------centos02--------------------------win10(客户机)  Ip:192.168.100.10/24     ip:192.168.10......