vue2.0的初始化

时间:2023-03-09 13:10:07
vue2.0的初始化

vue2.0的初始化,使用 webpack构建工具生成的项目

直接上代码

main.js文件

// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router' Vue.config.productionTip = false /* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  render: h => h(App)
})

new vue:表示 创建了一个Vue对象的实例

el:'#app' :表示 将刚创建的Vue对象实例 挂载 指定的Dom元素中,本项目是index.html文件的<div id="app"></div>。

思想就像下面是使用jQuery、JS拼接菜单的Java项目中,JS中"变量html"把结果赋给了html元素nav

<ul id="nav" class="nav">

 function drawTopNav(data) {
var html = [];
$.each(data, function(i, menu) {
html.push('<li><div class="nav-header"><a iconClass='+menu.ICON_CLASS+' class="firstNode" id="firstNode'
+ menu.ID
+ '" href="'
+ (!menu.URL || menu.URL == 'null' ? 'javascript:void(0);'
: menu.URL) + '"><img src="'+menu.IMG_URL+'"><h2>'
+ menu.NAME + '</h2></a></div></li>'); firstArr[menu.ID] = menu;
});
$("#nav").html(html.join(''));
$(".firstNode").bind("click", firstNodeClicked);
$(".firstNode").first().click();
}
render: h => h(App):作用是渲染视图,作为填充的内容给挂载点el,这是vue2.0的写法
----
下面是vue1.0的写法
components: { App },
template: '<App/>'
components: { App },作用是 注册组件信息
template: '<App/>'作用是 简写的模板调用组件的标签
实际上这两行代码的作用是一样的,
作用是渲染视图,作为填充的内容给挂载点el
 

index.html文件

 <!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<title>test_vue</title>
</head>
<body>
<div id="app"></div>
<!-- built files will be auto injected -->
</body>
</html>

App.vue文件

 <template>
<div id="app">
<img src="./assets/logo.png">
<router-view/>
</div>
</template> <script>
export default {
name: 'App'
}
</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>

router文件下的index.js文件

 import Vue from 'vue'
import Router from 'vue-router'
import HelloWorld from '@/components/HelloWorld' Vue.use(Router) export default new Router({
routes: [
{
path: '/',
name: 'HelloWorld',
component: HelloWorld
}
]
})

components文件夹下的HelloWorld.vue文件

 <template>
<div class="hello">
<h1>{{ msg }}</h1>
<h2>Essential Links YYYYYY 2019</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://chat.vuejs.org" target="_blank">
Community 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: 'HelloWorld',
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>

最终显示的页面:

vue2.0的初始化