赞
踩
创建index.html
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> <link rel="stylesheet" href="../../plug-in/element-ui/index.css"> </head> <body> <div id='app'> {{message}} </div> <script src='https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js'></script> <script> var app = new Vue({ el: '#app', data: { message: 'hello', }, }) </script> </body> </html>
至此,一个简单的vue实例已经创建完成
开发vue项目,第三方组件库是必不可少的,以elementUi为例
<!-- 引入样式 -->
<link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
<!-- 引入组件库 -->
<script src="https://unpkg.com/element-ui/lib/index.js"></script>
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> <!-- 引入样式 --> <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css"> </head> <body> <div id='app'> <el-button @click='toDetail'>详情</el-button> <el-table :data="tableData" a> <el-table-column prop="date" label="日期" width="180"> </el-table-column> <el-table-column prop="name" label="姓名" width="180"> </el-table-column> <el-table-column prop="address" label="地址"> </el-table-column> </el-table> </div> <script src='https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js'></script> <!-- 引入组件库 --> <script src="https://unpkg.com/element-ui/lib/index.js"></script> <script> var app = new Vue({ el: '#app', data: { message: 'hello', tableData: [{ date: '2016-05-02', name: '王小虎', address: '上海市普陀区金沙江路 1518 弄' }, { date: '2016-05-04', name: '王小虎', address: '上海市普陀区金沙江路 1517 弄' }, { date: '2016-05-01', name: '王小虎', address: '上海市普陀区金沙江路 1519 弄' }, { date: '2016-05-03', name: '王小虎', address: '上海市普陀区金沙江路 1516 弄' }] }, }) </script> </body> </html>
//pageA.js
Vue.component('page-a', {
template: '<div>{{message}}</div>',
data() {
return {
message: '我是page-a'
}
},
created() {
console.log(this.$route)
}
})
如需添加样式,在pageA目录下新建pageA.css, 后在index.html中引入就可以了
<script src='../pageA/pageA.js'></script>
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> <!-- 引入样式 --> <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css"> </head> <body> <div id='app'> <page-a></page-a> </div> <script src='https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js'></script> <!-- 引入组件库 --> <script src="https://unpkg.com/element-ui/lib/index.js"></script> <script src='../pageA/pageA.js'></script> <script> var app = new Vue({ el: '#app', data: { message: 'hello', }, }) </script> </body> </html>
<script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
【注】路由组件的编写同普通组件的编写一致,这里依旧引用pageA组件
引入组件
<script src='../pageA/pageA.js'></script>
渲染组件
<template id='pagea'>
<page-a ></page-a>
</template>
注册路由
var routes = [ {path:'/pagea',component:{template: '#pagea'}}, ]; var router = new VueRouter({ routes: routes }) var app = new Vue({ el: '#app', router, data: { message: 'hello', }, created() { }, methods: { toDetail() { location.href = '../detail/detail.html' } } })
全部代码
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> <!-- 引入样式 --> <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css"> </head> <body> <div id='app'> <router-view></router-view> </div> <template id='pagea'> <page-a ></page-a> </template> <!-- 引入vue --> <script src='https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js'></script> <!-- 引入vue-router --> <script src="https://unpkg.com/vue-router/dist/vue-router.js"></script> <!-- 引入组件库 --> <script src="https://unpkg.com/element-ui/lib/index.js"></script> <!-- 引入组件 --> <script src='../pageA/pageA.js'></script> <script> var routes = [ {path:'/pagea',component:{template: '#pagea'}}, ]; var router = new VueRouter({ routes: routes }) var app = new Vue({ el: '#app', router, data: { message: 'hello', }, }) </script> </body> </html>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。