While diving into Vue with Vuex, I attempted to create a component but encountered this frustrating error:
vue.js:634 [Vue warn]: Unknown custom element: actividadescomponent - did you register the component correctly? For recursive components, make sure to provide the "name" option. (found in )
I experimented with various approaches but unfortunately couldn't crack the solution.
This is the snippet of HTML code that's causing trouble:
<!DOCTYPE html>
<html>
<head>
<title>Vue</title>
<!-- Vue -->
<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
<!-- Vuex -->
<script src="https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="740201110c34475a425a">[email protected]</a>/dist/vuex.js"></script>
</head>
<body>
<div id="caja-vue">
<actividadesComponent></actividadesComponent>
</div>
<script src="codigo.js"></script>
</body>
</html>
Below is the corresponding JavaScript code:
//components
Vue.component('actividadesComponent', {
template: /*html*/
` <div>
<h1>Hola mundo</h1>
</div>
`,
computed: {
...Vuex.mapState(['actividades'])
},
methods: {
...Vuex.mapActions(['llamarJson'])
}
});
//Vuex
const store = new Vuex.Store({
state: {
actividades: [],
programas: []
},
mutations: {
llamarJsonMutation(state, llamarJsonAction){
state.actividades = llamarJsonAction;
}
},
actions: {
llamarJson: async function(){
const data = await fetch('/Documentos/Calendario-academico/calendario-2021-prueba.json');
const actividades = await data.json();
commit('llamarJsonMutation', actividades);
}
}
});
//Vue instance
new Vue({
el: '#caja-vue',
store: store
});