【Vue3】實作alert彈跳視窗

Vue3

【Vue3】實作alert彈跳視窗

Vue3


彈跳視窗

1
2
3
4
<div id="app">
  <button type="button" @click="openAlert">顯示跳窗</button>
  <alert-component v-if="showAlert" @emit-alert="closeAlert" :title="alertText"></alert-component>
</div>

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
const app = Vue.createApp({
  data(){
    return{
      showAlert: false,
      alertText: "父元件的標題名稱"
    }
  },
  methods:{
    openAlert(){
      this.showAlert = true;
    },
    closeAlert(){
      this.showAlert = false;
    }
  }
})

app.component('alert-component',{
  props:["title"],
  methods:{
    emitAlert(){
      this.$emit("emit-alert")
    }
  },
  template:`
  <div class="alert alert-primary">{{ title }}
    <button type="button" class="close" @click="emitAlert">&times;</button>
  </div>
  `
})

app.mount("#app");

Vue3 

其他相關