Vue.component ajax load template

Former-commit-id: fc6759db9f05a9f14c5996a9090636fd9c1e0f99
TangShanKaiPing
wanggang 6 years ago
parent 1aa43eab9f
commit 4f1a38ad34

@ -0,0 +1 @@
<div>foo {{count}}</div>

@ -1,11 +1,27 @@
const Foo = Vue.component('page-about', {
template: '<div>foo {{count}}</div>',
data() {
return {
count: 123
};
},
created: function () {
alert('created');
}
//const Foo = Vue.component('fool', {
// template: '<div>foo {{count}}</div>',
// data() {
// return {
// count: 123
// };
// },
// created: function () {
// alert('created');
// }
//});
const Foo = Vue.component('fool', function (resolve, reject) {
// 可以请求一个html文件既然存放模板还是html文件存放比较好
$.get("./test.c1.html").then(function (res) {
resolve({
template: res,
data() {
return {
count: 123
};
},
created: function () {
alert('created');
}
})
});
});

@ -18,35 +18,22 @@
<!-- 路由匹配到的组件将渲染在这里 -->
<router-view></router-view>
</div>
<script type="text/javascript" src="lib/vue/vue.min.js"></script>
<script type="text/javascript" src="lib/vue-router/vue-router.min.js"></script>
<script src="lib/jquery/jquery.min.js"></script>
<script src="lib/vue/vue.min.js"></script>
<script src="lib/vue-router/vue-router.min.js"></script>
<script type="text/javascript" src="test.c1.js"></script>
<script type="text/javascript" src="test.c2.js"></script>
<script>
// 1. 定义 (路由) 组件。
// 可以从其他文件 import 进来
// 2. 定义路由
// 每个路由应该映射一个组件。 其中"component" 可以是
// 通过 Vue.extend() 创建的组件构造器,
// 或者,只是一个组件配置对象。
// 我们晚点再讨论嵌套路由。
const routes = [
{ path: '/foo', component: Foo },
{ path: '/bar', component: Bar }
]
// 3. 创建 router 实例,然后传 `routes` 配置
// 你还可以传别的配置参数, 不过先这么简单着吧。
const router = new VueRouter({
routes // (缩写) 相当于 routes: routes
routes
})
// 4. 创建和挂载根实例。
// 记得要通过 router 配置参数注入路由,
// 从而让整个应用都有路由功能
const app = new Vue({
router
}).$mount('#app')

Loading…
Cancel
Save