Vue3, child component doesn't render dynamic string value [duplicate] - vuejs3

This question already has answers here:
ref vs reactive in Vue 3?
(7 answers)
Closed last month.
When I import HelloWorld component into App.vue, I'm unable to see the content of contenutoHeader.
HelloWorld:
<template>
<h1>{{ contenutoHeader }}</h1>
</template>
<script>
const contenutoHeader = "Sto funzionando";
export default {
name: "HelloWorld",
};
</script>
App.vue
<template>
<div>
<HelloWorld />
</div>
</template>
<script setup>
import HelloWorld from "./components/HelloWorld.vue";
</script>
Any suggestion?

You need to add your state to the data, like so:
<template>
<h1>{{ contenutoHeader }}</h1>
</template>
<script>
export default {
name: "HelloWorld",
data () {
return {
contenutoHeader: "Sto funzionando"
}
}
};
</script>
Anything within the data is reactive and can be used in your template

Related

Vuetify 3 - Cannot Import Async Components

I know this isn't the best time to use vuetify 3 but, i did. Hoping everything goes well until i found out that a lot of components are still missing.
Now, I am having a problem importing a lazy component using defineAsyncComponent.
my DOM doesn't seem to recognize async components.
I don't know if it is a vuetify error but my project is made out of vuetify so I was suspecting it was the problem.
Below is my code in dashboardACtionsLayout.vue
<template>
<div>
<div class="d-flex mb-3 mt-2">
<add-customer-modal />
</div>
</div>
</template>
<script setup>
import { defineAsyncComponent } from "vue";
components: {
addCustomerModal: defineAsyncComponent(() =>
import("#/components/customer/addCustomerModal.vue")
);
}
</script>
<style lang="scss" scoped></style>
and this is the error i am getting in my console:
Use defineAsyncComponent like below.
<template>
<div>
<div class="d-flex mb-3 mt-2">
<add-customer-modal />
</div>
</div>
</template>
<script setup>
import { defineAsyncComponent } from "vue";
const AddCustomerModal = defineAsyncComponent(() =>
import('#/components/customer/addCustomerModal.vue')
)
</script>
<style lang="scss" scoped></style>
note:
The name of your component is addCustomerModal, while vue's recommendation is that the beginning of all words should be capitalized, like AddCustomerModal.
It is now fixed as of #m kh answer. But when I try to register two components using this code; `
const AddCustomerModal = defineAsyncComponent(() =>
import("#/components/customer/addCustomerModal.vue")
);
const CustomersModal = defineAsyncComponent(() => {
import("#/components/customer/customersModal.vue");
});
` I will get an error like this

Vue: CMS API call to generate an array of content components (defineAsyncComponent) - Error Catching

GOAL: get API call from a headless CMS that returns a JSON with an array of content components with their settings and content
now, the CMS does not know if the components are available
neither does the APP, it tries to be dynamic
so, I am using vite / vue 3 and composition API
to get a component loading I am using this pattern
<script setup lang="ts">
const AdminPage = defineAsyncComponent(() =>
import('./components/AdminPageComponent.vue')
);
<script>
<template>
<AdminPage />
</template>
if I try to use directly assigned component it all works fine. The problem is when I am trying to use an array for it
<script setup lang="ts">
const contentPlaceholders = ref<any[]>([]);
const error = ref<any>(false)
const { data, status } = await axios.get('/get-content');
if(status === 200) {
data.forEach((placeholder: any) => {
contentPlaceholders.value.push({
component: defineAsyncComponent(() => import(`#/components/${kebabCase(placeholder.componentName)}/${placeholder.componentName}.vue`)
),
})
});
}
<script>
<template>
<Suspense>
<div>
<template v-for="placeholder in contentPlaceholders">
<component :is="placeholder.component"></component>
</template>
</div>
<template #fallback>
<div>Loading...</div>
</template>
</Suspense>
</template>
I never get components rendered... even if I do just
<template>
<Suspense>
{{ contentPlaceholders }}
...
</Suspense>
</template>
If I do it with a fix array like this...
it works fine... unless the component does not exist
<script setup lang="ts">
import { kebabCase } from 'lodash'
let contentPlaceholders = shallowRef<any[]>([])
const comps: string[] = ['RichTitle', 'RichText', 'NonExistant']
comps.forEach((cmp: string) => {
try {
contentPlaceholders.value.push({
component: defineAsyncComponent(() => import(`#/components/${kebabCase(cmp)}/${cmp}.vue`)),
})
} catch (e) {}
})
</script>
<template>
<Suspense>
<div>
<template v-for="placeholder in contentPlaceholders">
<component :is="placeholder.component"></component>
</template>
</div>
<template #fallback>
<div>Loading...</div>
</template>
</Suspense>
</template>
So how can I catch errors and make sure the app does not break when trying to import an non existent component?

Vue3 dynamic template

I am trying to create a dynamic Vue3 template but template is not rendering.
import App from "./App.vue";
const app = createApp(App)
app.component("DynamicData", {
template: "#dynamic-data",
data: function () {
return {}
}
});
app.mount("#app");
And laravel blade file.
<div id="app"></div>
<script type="vue-template" id="dynamic-data">
<div>My Custom Template</div>
</script>
and this is how i am calling the template.
I am using <component :is=""> because these will be dynamic component.
<component :is="DynamicData"></component>

How to import css file from node_modules in Vue-component

I want to connect this toastr-library into my component:
my-component.vue
<script>
import toastr from 'toastr';
export default {
mounted(){
toastr.success('Hello from toastr!');
}
}
</script>
<template>
<div>
Template for my-component.vue here.
</div>
</template>
<style>
/* HOW TO IMPORT `toastr.css` FROM NODE_MODULES HERE?
</style>
How to connect library's css from node_modules directory?
As #LinusBorg suggested here in vue-loader discussion thread, you can use src-attribute inside <style>-tag:
my-component.vue
<script>
import toastr from 'toastr';
export default {
mounted(){
toastr.success('Hello from toastr!');
}
}
</script>
<template>
<div>
Template for my-component.vue here.
</div>
</template>
<style src='toastr/build/toastr.min.css'></style>

Polymer 1.4 dom-bind defining properrties

In the index.html I use:
<template is="dom-bind" id="app">
In a normal dom-module I use:
<script>
(function() {
'use strict';
Polymer({
is: 'my-xxxx',
properties: {
/* location for properties */
}
Where do I define the properties used in the template dom-bind (f.i to have an observer attached to them?
Based on Polymer docs, you'd use JavaScript (inline or imported in index.html) to add template properties inside of an event handler for the template's dom-change event. For example, you could add a message property to the template with this script:
var t = document.getElementById('app');
t.addEventListener('dom-change', function() {
t.message = 'Hello world!';
});
See demo below:
<head>
<base href="https://polygit.org/polymer+:master/components/">
<script src="webcomponentsjs/webcomponents-lite.min.js"></script>
<link rel="import" href="polymer/polymer.html">
</head>
<body>
<template is="dom-bind" id="app">
{{message}}
</template>
<script>
var t = document.getElementById('app');
// The dom-change event signifies when the template has stamped its DOM.
t.addEventListener('dom-change', function() {
// auto-binding template is ready.
t.message = 'Hello world!';
});
</script>
</body>
jsbin

Resources