亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Vue中的components組件與props的使用解讀

 更新時間:2023年05月17日 09:54:43   作者:代碼の搬運工  
這篇文章主要介紹了Vue中的components組件與props的使用解讀,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

components組件與props的使用

vue-cli 3.0 項目,使用components組件化開發(fā),可以減少不必要重復(fù)的代碼

初始化vue-cli3.0的項目的結(jié)構(gòu)目錄下的src下會有個components文件夾

init

建議:在components中創(chuàng)建對應(yīng)模塊的文件夾,存放該模塊的公共組件

在loginReg.vue文件夾中寫上代碼,通過props父傳子的方式,那需要接收父組件的值使用 綁定:屬性名="變量"的形式。并且在props中定義

以下是loginReg.vue 文件:

<template>
  <div class="loginRegComponent">
    <div class="titleBox">
      <p class="title-left"></p>
      <p v-if="title" class="title-center">{{title}}</p>
      <p class="title-right" v-if="lesgo=='去注冊'" @click="$router.push('/register')">{{lesgo}}</p>
      <p class="title-right" v-else @click="$router.push('/login')">{{lesgo}}</p>
    </div>
    <input type="text" v-model="userinfo.username" class="username input-group form-control glyphicon glyphicon-user" :placeholder="placeholderUser">
    <input type="password" v-model="userinfo.password" class="password input-group form-control" :placeholder="placeholderPsw">
    <div>
      <input type="text" v-model="userinfo.code" class="code input-group form-control" :placeholder="placeholderCode">
      <span class="codeNumber"></span>
    </div>
    <button class="btn btn-primary" @click="btnSubmit">{{btnText}}</button>
  </div>
</template>
<script>
export default {
  props: {
    title: String,
    placeholderUser:String,
    placeholderPsw: String,
    placeholderCode: Number,
    btnText:{
      default: "text", //可以傳個默認值
      type:String 		//類型
	},
    lesgo: String,
  },
  data() {
    return {
      userinfo: {
        username: null,
        password: null,
        code: null,
      },
    };
  },
  methods: {
    btnSubmit() { //發(fā)送事件  數(shù)據(jù)
      this.$emit("btnSubmit", this.userinfo);
    },
  },
};
</script>

在login.vue 引入寫好的loginReg.vue組件,通過props的參數(shù)名來對應(yīng)的設(shè)置值

注冊頁面也是對飲的引入<login-reg> 的方式進行傳參,在不需要顯示對應(yīng)的input框的時候可以使用v-if來判斷,存在值才顯示。

login.vue頁面:

<template>
  <div id="login" class="login" :style="{height:DocHeight+'px'}">
    <div class="container-fluid">
      <login-reg 
	      title="登錄" 
	      placeholderUser="請輸入賬號" 
	      placeholderPsw="請輸入密碼" 
	      placeholderCode="請輸入驗證碼" 
	      btnText="登錄" 
	      @btnSubmit="btnSubmit"  //接收事件
	      lesgo="去注冊"
      >
      </login-reg>
    </div>
  </div>
</template>
<script>
// 通過import的方式導(dǎo)入
import loginReg from "@/components/loginReg/loginReg.vue";
export default {
  name: "login",
  components: {//注冊組件
    loginReg,
  },
  data() {
    return {};
  },
  methods: {
    btnSubmit(data) {//接收子組件發(fā)送過來的事件/數(shù)據(jù)
      console.log(data);
    },
  }

效果圖

vue中props的默認寫法

默認寫法

? props: {
? ? rowClick: {
? ? ? type: Function,
? ? ? default: function() {}
? ? },
? ? title: {
? ? ? type: String,
? ? ? default: "標題"
? ? },
? ? display: {
? ? ? type: String,
? ? ? default: "table"?
? ? },
? ? columnCount: {
? ? ? type: Number,
? ? ? default: 4
? ? },
? ? columns: {
? ? ? type: Array,
? ? ? default() {
? ? ? ? return [];
? ? ? }
? ? },
? ? showPage: {
? ? ? type: Boolean,
? ? ? default: true
? ? },
? ? api: {
? ? ? type: Object,
? ? ? default() {
? ? ? ? return {};
? ? ? }
? ? },
? ? parameter: {
? ? ? type: Object,
? ? ? default() {
? ? ? ? return {};
? ? ? }
? ? },
? ? defaultParameter: {
? ? ? type: Boolean,
? ? ? default() {
? ? ? ? return true;
? ? ? }
? ? }
? },

注意:

如果默認值得類型為數(shù)組或者對象,一定要在函數(shù)中返回這個默認值,而不是直接寫,否則報錯

正確:

?menu:{
? ? ?type:Array,
? ? ?default:function(){
? ? ? ? ?return []
? ? ?}
?}

錯誤

?menu:{
? ? ?type:Array,
? ? ?default:[]
?}

或者直接跟上面第一個代碼一樣,不管什么類型,都寫在函數(shù)返回中。 

總結(jié)

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Vue3安裝dataV報錯問題解決方案

    Vue3安裝dataV報錯問題解決方案

    這篇文章主要給大家介紹了關(guān)于Vue3安裝dataV報錯問題解決方案的相關(guān)資料,dataV用于大屏展示,個人覺得比echarts簡單很多,需要的朋友可以參考下
    2023-06-06
  • vue中provide?inject的響應(yīng)式監(jiān)聽解決方案

    vue中provide?inject的響應(yīng)式監(jiān)聽解決方案

    這篇文章主要介紹了vue中provide?inject的響應(yīng)式監(jiān)聽解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-04-04
  • vue3+ElementPlus封裝函數(shù)式彈窗組件詳解

    vue3+ElementPlus封裝函數(shù)式彈窗組件詳解

    這篇文章主要為大家詳細介紹了如何利用vue3和ElementPlus封裝函數(shù)式彈窗組件,文中的示例代碼講解詳細,感興趣的小伙伴可以了解一下
    2023-08-08
  • vue 移動端注入骨架屏的配置方法

    vue 移動端注入骨架屏的配置方法

    骨架屏就是在頁面未渲染完成的時候,先用一些簡單的圖形大致勾勒出頁面的基本輪廓,給用戶帶來更好的體驗。這篇文章主要介紹了vue 移動端注入骨架屏,需要的朋友可以參考下
    2019-06-06
  • Vue數(shù)據(jù)驅(qū)動模擬實現(xiàn)1

    Vue數(shù)據(jù)驅(qū)動模擬實現(xiàn)1

    這篇文章主要介紹了Vue數(shù)據(jù)驅(qū)動模擬實現(xiàn)的相關(guān)資料,允許采用簡潔的模板語法聲明式的將數(shù)據(jù)渲染進DOM,且數(shù)據(jù)與DOM綁定在一起,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-01-01
  • Vue router/Element重復(fù)點擊導(dǎo)航路由報錯問題及解決

    Vue router/Element重復(fù)點擊導(dǎo)航路由報錯問題及解決

    這篇文章主要介紹了Vue router/Element重復(fù)點擊導(dǎo)航路由報錯問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-07-07
  • vue全局接入百度地圖的實現(xiàn)示例

    vue全局接入百度地圖的實現(xiàn)示例

    本文主要介紹了vue全局接入百度地圖的實現(xiàn)示例,文中根據(jù)實例編碼詳細介紹的十分詳盡,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • 關(guān)于vue-i18n在單文件js中的使用

    關(guān)于vue-i18n在單文件js中的使用

    這篇文章主要介紹了關(guān)于vue-i18n在單文件js中的使用,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-09-09
  • vue項目之前端CryptoJS加密、解密代碼示例

    vue項目之前端CryptoJS加密、解密代碼示例

    在Vue項目中集成CryptoJS進行數(shù)據(jù)加密,首先需要通過npm安裝CryptoJS安裝包,然后在項目文件中引入CryptoJS,文中通過代碼介紹的非常詳細,需要的朋友可以參考下
    2024-11-11
  • 公共Hooks封裝文件下載useDownloadFile實例詳解

    公共Hooks封裝文件下載useDownloadFile實例詳解

    這篇文章主要為大家介紹了公共Hooks封裝文件下載useDownloadFile實例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-11-11

最新評論