vue-cli
使用Vue-cli
首先,我们可以使用
$ npm install -g vue-cli
安装完毕之后,我们可以使用如下方式来初始化新的项目
$ vue init webpack vueapp01
这里我们让
到这里项目的模板文件被添加到了
$ npm run dev
该命令会启动一个监听
如果你希望将项目打包出开发版本,可以使用
$ npm run build
项目结构
该部分的代码参考vue-boilerplate。首先我们来看下
进入项目根目录之后我们使用npm intsall
命令安装所有依赖,所有的依赖被声明在package.json
文件中。文件index.html
中包含了如下
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>vueapp01</title>
</head>
<body>
<style>
.sidebar {
margin-top: 48px;
}
#nav {
position: fixed;
z-index: 9;
padding: 0 8px;
display: flex;
justify-content: space-evenly;
align-items: center;
width: 300px;
top: 0;
height: 60px;
}
</style>
<div id="nav">
<h3><a href="https://ng-tech.icu/books-gallery">Books</a></h3>
<span style="margin:0 8px;display:inline-block">|</span>
<h3><a href="https://github.com/FE-Kits">FE-Kits</a></h3>
<span style="margin:0 8px;display:inline-block">|</span>
<h3><a href="https://github.com/BE-Kits">BE-Kits</a></h3>
<span style="margin:0 8px;display:inline-block">|</span>
<h3><a href="https://github.com/AI-Kits">AI-Kits</a></h3>
</div>
<div id="app"></div>
<!-- built files will be auto injected -->
</body>
</html>
该文件是整个应用的入口点,注意,无论你把<div>
元素放在哪,只要保证其id
属性为app
即可,该元素是整个由
new Vue({
el: "#app",
template: "<App/>",
components: { App },
});
文件首部我们发现两个引入语句:
import Vue from 'vue'
:Vue 是整个框架的主类import App from './App'
:App 是整个应用的根元素
使用new
关键字能够创建
el: 设定Vue 应用的DOM 挂载点template: 包含HTML 代码的模板components: 用于模板中的Vue.js 组件
该模板仅包含一个元素:<App />
,当然这并不是App.vue
文件中:
<template>
<div id="app"> <img src="./assets/logo.png" /> <hello></hello> </div>
</template>
<script>
import Hello from "./components/Hello";
export default {
name: "app",
components: {
Hello,
},
};
</script>
<style>
#app {
font-family: 'Avenir', Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
</style>
对于每个
<template></template>
: Component’s template code<script></script>
: Component’s script code<style></style>
: Component’ CSS code
我们先看看template
与script
这两块。script
块导出了某个声明为app
的组件,该组件中的属性声明了对于Hello
组件的引用。Hello
组件则是被定义在hello.vue
文件中,为了使用其他组件我们同样需要在script
首部引入该组件。整个
<template>
<div class="hello">
<h1>{{ msg }}</h1>
<h2>Essential Links</h2>
<ul>
<li><a href="https://vuejs.org" target="_blank">Core Docs</a></li>
<li><a href="https://forum.vuejs.org" target="_blank">Forum</a></li>
<li>
<a href="https://gitter.im/vuejs/vue" target="_blank">Gitter Chat</a>
</li>
<li><a href="https://twitter.com/vuejs" target="_blank">Twitter</a></li>
<br />
<li>
<a href="http://vuejs-templates.github.io/webpack/" target="_blank"
>Docs for This Template</a
>
</li>
</ul>
<h2>Ecosystem</h2>
<ul>
<li><a href="http://router.vuejs.org/" target="_blank">vue-router</a></li>
<li><a href="http://vuex.vuejs.org/" target="_blank">vuex</a></li>
<li>
<a href="http://vue-loader.vuejs.org/" target="_blank">vue-loader</a>
</li>
<li>
<a href="https://github.com/vuejs/awesome-vue" target="_blank"
>awesome-vue</a
>
</li>
</ul>
</div>
</template>
<script>
export default {
name: "hello",
data() {
return {
msg: "Welcome to Your Vue.js App",
};
},
};
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
h1,
h2 {
font-weight: normal;
}
ul {
list-style-type: none;
padding: 0;
}
li {
display: inline-block;
margin: 0 10px;
}
a {
color: #42b983;
}
</style>