How to mount two seperate applications inside one Vue project? - vue.js

I am creating a frontend and a backend application inside one Vue 3 project. Everything works fine except that app.js is throwing following error, depending on which component is visited in the browser.
I you visit the app.vue:
[Vue warn]: Failed to mount app: mount target selector "#cms" returned null.
and if you visit the cms.vue:
[Vue warn]: Failed to mount app: mount target selector "#app" returned null.
The mounting file app.js looks like this:
require('./bootstrap');
import {createApp} from 'vue';
import app from "../vue/app";
import cms from "../vue/cms";
import router from "./router";
createApp(app)
.use(router)
.mount("#app");
createApp(cms)
.use(router)
.mount("#cms");
Is there any way I can prevent the browser warning?

You could query the document for those elements, and only mount the ones that exist:
const appEl = document.querySelector('#app');
if (appEl) {
createApp(app).use(router).mount(appEl);
}
const cmsEl = document.querySelector('#cms');
if (cmsEl) {
createApp(cms).use(router).mount(cmsEl);
}

Related

Storybook: [vue-composition-api] must call Vue.use(VueCompositionAPI)

I've been trying to get storybook working with a Vue 2 + composition API project for a while now and I keep running into the error:
Unexpected error while loading ./ProductCard.stories.js: [vue-composition-api] must call Vue.use(VueCompositionAPI) before using any function.
So I assume I need to include it before any file/story gets called.
When I try and import it into my storybook's main.js file like this:
import Vue from "vue";
import VueCompositionApi from "#vue/composition-api";
Vue.use(VueCompositionApi);
module.exports = {
//... storybook config
}
I get the console error:
import Vue from "vue";
^^^^^^
SyntaxError: Cannot use import statement outside a module
I've also tried:
const Vue = require("vue");
const VueCompositionApi = require('#vue/composition-api');
Vue.use(VueCompositionApi);
Which appears to work but I still get the top error [vue-composition-api] must call Vue.use(VueCompositionAPI) before using any function.
Does anybody know where I'm meant to import the composition API so it's loaded before it's called?
I've tried putting it in the preview.js file as well with the same error.
Whats causing it is the component ProductCard loads a composable called useUiStatewhich uses vue-composition-api.
I would have thought it's called vue.use already.

[Vue warn]: Property "$primevue" was accessed during render but is not defined on instance

Case and problem
I´m working on a private project with Vue.js and have the following error, which occurs when I´m trying to use the FileUpload component of PrimeVue:
[Vue warn]: Property "$primevue" was accessed during render but is not defined on instance.
Trying to use FileUpload in my component:
<template>
<FileUpload name="demo[]" url="" #upload="onUpload" :multiple="true" :maxFileSize="1000000">
<template #empty>
<p>Drag and drop files to here to upload.</p>
</template>
</FileUpload>
</template>
The error only occurs, when I try to use FileUpload, if I remove it, the component works. FileUpload and PrimeVue are imported like they should, in the main.js:
import {createApp} from 'vue'
import router from "./router";
import store from "./store";
import PrimeVue from "primevue/config";
import PrimeIcons from "primevue/config";
import App from "./App";
const app = createApp(App);
app.use(
router,
PrimeVue,
PrimeIcons,
store
)
import 'primevue/resources/primevue.min.css'
import 'primeflex/primeflex.css'
import 'primeicons/primeicons.css'
import 'primevue/resources/themes/bootstrap4-dark-purple/theme.css'
import Card from "primevue/card";
import Menubar from "primevue/menubar";
import FileUpload from "primevue/fileupload";
app.component('Card', Card)
app.component('Menubar', Menubar)
app.component('FileUpload', FileUpload)
app.mount('#app')
What I tried so far
I searched this issue, but the only exact match for this error is an old closed issue on GitHub regarding the Calendar component: Issue #808. The error in this issue was caused because of the breaking change with the new PrimeVue API. This should not be my case, because it was introduced with V3.1 and I´m using V3.7.
In case the version is the problem I tested different versions of PrimeVue, like 3.1, 3.2, 3.3 but the error still shows. Thats why the actual dependencie is still the latest:
"primevue": "^3.7.0"
Maybe there is an already existing solution on SO or Google, but either my english is to bad to understand or I´m still to fresh at Vue.js to comprehend the problem.
Thanks in advance!
Your usage of app.use() is incorrect:
app.use(
router,
PrimeVue,
PrimeIcons,
store
)
app.use() takes only two arguments:
first argument: the Vue plugin
second argument: the plugin options
Also, PrimeIcons is not a plugin, so that should not be passed to app.use().
Solution
Pass each plugin individually to app.use():
app.use(router)
.use(PrimeVue)
//.use(PrimeIcons) // not a plugin
.use(store)

Vue.js 3 telepone field with country code input

Anyone know a way to insert a telephone field with a country code dropdown similar to the one in the link for vue3?
https://www.npmjs.com/package/vue-tel-input
I had tried to use the package in the link but it throws the following error in console.
Uncaught (in promise) TypeError: selfHook.bind is not a function
attempted implementation:
main.js
import { createApp } from 'vue'
import VueTelInput from 'vue-tel-input'
const app = createApp(App);
app.use(VueTelInput)
app.mount('#app');
in the App.vue I just imported it like a component using the following code:
<vue-tel-input></vue-tel-input>
Someone posted on an open issue regarding Vue3 support for the library that they have created a Vue3 version of it: https://github.com/victorybiz/vue3-tel-input
I have tried it and it seems to work fine!
This library supports Vue 3 now. https://iamstevendao.github.io/vue-tel-input/documentation/next.html
npm install vue-tel-input#next
import { createApp } from 'vue';
import App from './App.vue';
import VueTelInput from 'vue-tel-input';
import 'vue-tel-input/dist/vue-tel-input.css';
const app = createApp(App);
app.use(VueTelInput);
app.mount('#app');
...
<template>
<vue-tel-input v-model="phone"></vue-tel-input>
></template>

Uncaught TypeError: at.a is undefined

I have a Vue 2 web app that uses AWS Amplify. I have another app almost exactly like it in every way and it works fine, but for some reason I cannot get this one to work in production.
Locally in dev, everything works just fine.
When I build and launch it with Express, it doesn't load the website. Only when I remove "Amplify.configure(awsconfig);" from src/main.js does it load, but, of course, none of the AWS related functionality (API calls) work.
I get the following error on the console:
Uncaught TypeError: at.a is undefined
56d7 main.js:12
Webpack 6
At main.js line 12 is "Amplify.configure(awsconfig);"
/* eslint-disable */
import Vue from "vue";
import App from "./App.vue";
import router from "./router.js";
import Amplify from "aws-amplify";
// Material plugins
import MaterialKit from "./plugins/material-kit";
import awsconfig from "./aws-exports";
Amplify.configure(awsconfig);
Vue.config.productionTip = false;
Vue.use(MaterialKit);
new Vue({
router,
render: h => h(App)
}).$mount("#app");
The site is very simple and is set up just like my other one that works, so this is very confusing.
Any idea what the issue could be? Let me know if you need any other code snippets.
Where I import Amplify at the top, it shouldn't be
import Amplify from "aws-amplify";
but instead
import { Amplify } from "aws-amplify";
This is strange because my other project does the first version and works fine.

Nuxt: Vue Instance for Global Events

I'd like to use a separate Vue instance to handle events. This approach works in a standard Vue app but throws an error in the Nuxt environment.
Do I need to simply reference it differently?
Code
const Vue = require('vue');
const Hub = new Vue();
export default Hub;
// Usage
import Hub from '~/events/hub';
Hub.$emit(EVENT_TOGGLE_NAVIGATION, true);
Error
Uncaught TypeError: Vue is not a constructor
Environment
nuxt 1.0.0
vue 2.5.17
You need to use import
import Vue from 'vue'