blog

Vue.extendラップメソッドを使うタイミングを覚えておく

メソッド:メッセージアラートボックス:呼び出し...

Aug 15, 2020 · 2 min. read
シェア

メソッド

import Vue from 'vue';
export default function create(component, props) {
 const Ctor = Vue.extend(component);
 const div = document.createElement('div');
 document.body.appendChild(div);
 const ctor = new Ctor({
 propsData: {
 ...props
 }
 }).$mount(div);
 ctor.remove = () => {
 ctor.$el.remove();
 ctor.$destroy();
 }
 return ctor;
}

メッセージアラートボックス

<template>
 <div class="box" v-if="isShow">
 <h2>{{title}}</h2>
 <p class="box-content">{{message}}</p>
 </div>
</template>
<script>
export default {
 props: {
 title: {
 type: String,
 default: ""
 },
 message: {
 type: String,
 default: ""
 },
 duration: {
 type: Number,
 default: 1000
 }
 },
 data() {
 return {
 isShow: false
 };
 },
 methods: {
 show() {
 this.isShow = true;
 setTimeout(this.hide, this.duration);
 },
 hide() {
 this.isShow = false;
 this.remove();
 }
 }
};
</script>
<style>
.box {
 position: fixed;
 width: 100%;
 top: 16px;
 left: 0;
 text-align: center;
 pointer-events: none;
 background-color: #fff;
 border: grey 3px solid;
 box-sizing: border-box;
}
.box-content {
 width: 200px;
 margin: 10px auto;
 font-size: 14px; 
 padding: 8px 16px;
 background: #fff;
 border-radius: 3px;
 margin-bottom: 8px;
}
</style>

呼び出し

create(Notice, {
 title: 'メッセージアラート,
 message: ' 
}).show()
Read next

約束

関数の実行中のPromiseは同期ですが、call resolveメソッドの最後の非同期操作や、エラーcall rejectメソッドの途中で遭遇する非同期操作があり、いずれもEventLoopにマイクロタスクとして追加されます。また、なぜマイクロタスクがあるかというと、同期的な方法は、タスクが発生した場合、プログラム全体の実行に影響を与えるため、最初の方法は好ましくありません。

Aug 15, 2020 · 2 min read

JavaScript入門

Aug 14, 2020 · 1 min read

オープンサイン

Aug 14, 2020 · 1 min read

leetcodeのGo言語実装

Aug 14, 2020 · 1 min read