To create our first SFC web page from scratch we will:
Now we will remove all content in the example project we made on the previous page to create our own simple web page in Vue.
Before we start to write code, remove all content inside the <template>
, <script>
and <style>
tags, and remove any attributes like 'setup' or 'scoped'.
Your 'App.vue' file should now look like this:
App.vue:
<script></script>
<template></template>
<style></style>
Also remove the folders 'assets' and 'components' inside the 'src' folder.
Remove the line where assets are imported inside the 'main.js' file so that 'main.js' looks like this:
main.js:
import { createApp } from 'vue'
import App from './App.vue'
createApp(App).mount('#app')
We now have an empty project to work with.
Now that we have a clean project, add a header inside the <template>
tag, like this:
<template>
<h1>Hello World!</h1>
</template>
<script></script>
<style></style>
Save the 'App.vue' file, go to your browser by following the localhost link in the terminal. Do you see the result? The browser should now update automatically every time you save a change in VS Code, without having to manually refresh the browser.
Now lets take a look at a slightly larger Vue example:
App.vue
:
<template>
<h1>{{ message }}</h1>
</template>
<script>
export default {
data() {
return {
message: 'This is some text'
};
}
};
</script>
<style></style>
Run Example »
Note: In the example above, export default
makes it possible for 'main.js' to catch the data with the import App from './App.vue'
so that it can be mounted on the <div id="app">
tag inside 'index.html'.
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!