Vue 封裝input 搜索組件


新建一個souso組件

 1 <template>
 2   <div :class="{'inline':inline}">
 3     <span v-if="this.label">{{this.label}}</span>
 4     <input
 5       type="text"
 6       :value="value"
 7       @input="$emit('input',$event.target.value)"    
 8       :placeholder="placeholder"
 9     />
10   </div>
11 </template>
12 <script>
13 export default {
14   props: {
15     label: String,
16     value: String,
17     placeholder: String,
18     inline: {                        //使input變為行內元素,默認false
19       type: Boolean,
20       default: false
21     }
22   }
23 };
24 </script>
25 <style scoped>
26 .inline {
27   display: inline;
28 }
29 span {
30   margin-right: 10px;
31 }
32 </style>

在頁面上調用

 1 <template>
 2   <div>
 3  <souso inline v-model="user" placeholder="請輸入內容" @input="input"></souso>
 4    <button @click="submit">提交</button>
 5   </div>
 6 </template>
 7 
 8 <script>
 9 import souso from './components/souso'
10 export default {
11   name: "App",
12   components: {
13       souso
14   },
15   data() {
16     return {
17      user:'',
18     };
19   },
20   methods:{
21      input(value){
22             console.log(value)
23         },
24         submit(){
25             console.log(this.user)
26         }
27   }
28 };
29 </script>
30 
31 <style lang="less">
32 
33 </style>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM