Creating the Component
In the `components` folder, create a file named AppModal.vue
. You can do this by using the New > Vue file wizard accessible from the project’s context menu. We’ll create a very basic component for now.
Note: For more on CodeMix’s Vue capabilities, please read this article.
<template>
<div>
<div>
<h1>Modal heading</h1>
</div>
<hr>
<div>
<h1>Modal content</h1>
</div>
<hr>
<div>
<button>Close</button>
</div>
</div>
</template>
<script>
export default {
name: 'app-modal',
};
</script>
In App.vue
, we import the `AppModal` component like below:
<template>
<div id="app">
<h1>Vue Modal Tutorial</h1>
<app-modal></app-modal>
</div>
</template>
<script>
import AppModal from './components/AppModal';
export default {
components: {
AppModal
}
}
</script>
<style>
#app {
font-family: "Avenir", Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
}
</style>
If you’re using a recent version of CodeMix, you’ve probably noticed a new browser pane beside the editor – this pane will update after you save your changes, to show you the current state of the app after your changes have been rebuilt. Do read more about LivePreview 2.0
here. Your application will now look like this:
Obviously this is no good as the modal is open over our app – we’re going to add a condition under which the modal will show, and a button to display it.
<template>
<div id="app">
<h1>Vue Modal Tutorial</h1>
<app-modal v-if="showModal"></app-modal>
<button @click="openModal" v-if="!showModal">Open Modal</button>
</div>
</template>
<script>
import AppModal from './components/AppModal';
export default {
components: {
AppModal
},
data() {
return {
showModal: false
}
},
methods: {
openModal() {
this.showModal = true;
}
},
}
</script>
Here we created our modal template with two `div` tags and one `button` element. The `button` is used to open our modal. In our second div
tag, we added a v-if=isOpen
directive so that our modal will only display if the `isOpen` property is true.