Kha*_*rul 0 html javascript vue.js
我是Vue.js 的新手。我想了解使用component. 我试图将我的组件导入另一个组件,但失败了。我正在使用Laravel 5.8. 以下是我收到的错误。
找不到模块:错误:无法解析'./components/modalAlert.vue
下面是我的代码。
应用程序.js
Vue.component('form-to-do', require('./components/formToDo.vue').default);
Vue.component('modal-alert', require('./components/modalAlert.vue').default);
const app = new Vue({
el: '#app',
});
Run Code Online (Sandbox Code Playgroud)
formToDo.Vue
<template>
// form
<modal-alert></modal-alert>
</template>
<script>
import modalAlert from './components/modalAlert.vue';
export default {
components: {
'modal-alert': modalAlert
},
data() {
return {
setErrors: [],
tasks: [],
task: {
id: '',
name: '',
completed: '',
date_completed: ''
},
result: '',
input: {
name: ''
}
};
},
}
</script>
Run Code Online (Sandbox Code Playgroud)
modalAlert.vue
<template>
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
Test
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
</template>
<script>
export default {
props: ['id'],
data() {
return {
}
},
mounted() {
console.log('Component mounted.')
}
}
</script>
Run Code Online (Sandbox Code Playgroud)
您的组件可能位于同一文件夹中。在你的组件 formToDo.vue 中: 改变这个:
import modalAlert from './components/modalAlert.vue';
Run Code Online (Sandbox Code Playgroud)
对此:
import modalAlert from './modalAlert.vue';
Run Code Online (Sandbox Code Playgroud)
为了解决另一个问题,正如@ambianBeing 所建议的,您的组件 formToDo.vue 必须具有根元素才能在其中添加子组件。
<template>
<div> <!-- this is the root -->
<modal-alert></modal-alert>
</div>
</template>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2399 次 |
| 最近记录: |