如何使用webpack从模板加载VueJS应用程序



我刚刚开始使用VueJS,目前正在尝试使用WebpackLoader将其添加到我的Django项目中。

我有以下两个文件:

App.vue

<template>
<div id="app">
<img alt="Vue logo" src="./assets/logo.png">
<HelloWorld msg="Welcome to Your first Vue.js App"/>
</div>
</template>
<script>
import HelloWorld from './components/HelloWorld.vue'

export default {
name: 'app',
components: {
HelloWorld
}
}


</script>

App02.vue

<template>
<div id="app02">
<img alt="Vue logo" src="./assets/logo.png">
<HelloWorld msg="Welcome to Your second Vue.js App0"/>
</div>
</template>
<script>
import HelloWorld from './components/HelloWorld.vue'
export default {
name: 'app02',
components: {
HelloWorld
}
}
</script>
<style>
#app02 {
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>

这是我的main.js:

import Vue from "vue/dist/vue.js";
import Vuex from "vuex";
import storePlugin from "./vuex/vuex_store_as_plugin";
import App from './App.vue'
Vue.use(Vuex);
Vue.use(storePlugin);
Vue.config.productionTip = false;
/* NOTE: unlike index.js, we are not passing props from our template, so the following render/mount 
syntax is ok */
new Vue({
render: h => h(App),
}).$mount('#app');

最后,这里是我的Django html模板:

{% load render_bundle from webpack_loader %}

{% block content %}
<h3>My second Vue app goes here</h3>
<div id="app">
<app></app>
</div>

{% render_bundle 'chunk-vendors' %}
{% render_bundle 'vue_app_02' %}
{% endblock %}

我的代码的问题是它似乎没有加载App02,因为当我加载html时,我会看到欢迎使用您的第一个Vue应用程序,而不是请使用您的第二个应用程序,尽管从我的模板中,我正在加载vue_app_02

我知道这个问题很基本,但我对此很陌生,一切都很混乱,我不明白我需要main.js和其他文件,如index.jsindex.html。欢迎任何建议

您必须将app02.vue添加到vue路由器中

这个网站可以帮助你创建vue路由器

https://saidhayani.medium.com/understand-routing-in-vue-js-with-examples-6da96979c8e3

最新更新