Can't include vue related npm package to my Laravel + Vue project - laravel

I want to include beatiful flash messages to mey Laravel + Vue app, so I found a npm package on GitHub and installed this in my app (npm install)
I'm beginner in VueJs and I'm facing a problem: I just can't use the package in my vue components since it throw errors when I try to include this
What I use and what I get:
it's my app.js code
window.Vue = require('vue');
/*including the package*/
import FlashMessage from '#smartweb/vue-flash-message';
Vue.use(FlashMessage);
/*including the package*/
Vue.component('settings-form',require('./components/SettingsFormComponent.vue').default);
Vue.component('profile-nav',require('./components/ProfileNavComponent.vue').default);
const app = new Vue({
el: '#app',
});
What error I get using the code:
"TypeError: Cannot read property 'flashMessage' of undefined at app.js:1863"
I run this method (flashMessage) when I want to output a message:
Also I tried to use this code in my app.js to include the packange:
Vue.component('FlashMessage', require('#smartweb/vue-flash-message').default);
but it doesn't work too, it throws this error:
"Failed to mount component: template or render function not defined ..."
I'm trying to use my component that way (in ProfileNavComponent.vue):
<FlashMessage></FlashMessage>
Could you please tell me what the problem can be in?
thanks guys for any help)
I really appreciate this!

Im an author of this package. If you will have some issues with it, please leave the issue on github: https://github.com/smwbtech/vue-flash-message/issues
About your problem, I suppose that it is function context issue. Try to use arrow function as callback for .then
axios.post('/api/post/', body)
.then( res => {
this.flashMessage(/*your message*/);
this.data = '';/*other actions with your component data*/
})
.catch(e);

I usually use vue packages adding them from the name of the package, try:
import FlashMessage from 'vue-flash-message';
And when you want to use the component, try to use FlashMessage.show().

That's how I uncluded the package into my project
in app.js I added that code:
import FlashMessage from '#smartweb/vue-flash-message';
Vue.use(FlashMessage, {tag: 'flash-message', strategy: 'multiple'});
And then in any component I wanted to use flashMessages I used code like this:
this.flashMessage.success({
title: 'New Friend!',
message: data.user_created_by.full_name + ' started chatting with you!',
time: 5000,
blockClass: 'image-uploaded-flash',
});
Thanks a lot guys all who wanted to help me!!!

Related

How to add scroll reveal in vue3

Could someone advise me to use like scroll reveal for vue3, I couldn't find any forum and if so please explain to me exactly how to import scroll reveal for vue3
I've already tried different libraries, but all are for vue3, and the transition belonging to vue3 wouldn't work very well as scrollreveal
enter image description here
The Vue 3 branch for vue-scroll-reveal is not released on NPM, but you can add the library as a dependency using the project's github's URL. You will need to install it together with the dependency "scrollreveal" with yarn or npm like so:
yarn add scrollreveal https://github.com/tserkov/vue-scroll-reveal#v2
or
npm i scrollreveal https://github.com/tserkov/vue-scroll-reveal#v2
The Github README also describes how to use the library in a component which I've pasted below:
If using default options
<script setup>
import { vScrollReveal } from 'vue-scroll-reveal';
</script>
OR if using custom default options
<script setup>
import { createScrollRevealDirective } from 'vue-scroll-reveal';
const vScrollReveal = createScrollRevealDirective({
delay: 1000,
duration: 150,
});
</script>

node_modules does not provide an export named 'Ref' on Vue.js component

I'm having a weird error. On a component I have, Ref is working correctly.
On a component I've just created, id doesn't. I'm calling it the same way on both.
import { Ref } from 'vue';
const isOpen = ref(false);
Giving the error:
Uncaught (in promise) SyntaxError: The requested module '/node_modules/.vite/deps/vue.js?v=f65e1616' does not provide an export named 'Ref'
I've tried running "npm run build" again, with the following error:
Non-existent export 'Ref' is imported from
node_modules/vue/dist/vue.runtime.esm-bundler.js
I don't really know what could be causing this. I work on this project on 2 different computers and I've cloned it a few days ago. I haven't encountered any issues until now
You have to change your import
Try this
import { ref } from 'vue';
You can see the documentation of Vue.js (template refs)
For anyone having this issue in the future, for some reason when writing "import ref" your code editor may wrongly autocomplete it with capitalized { Ref } instead of the right { ref }

How to use browser.storage.sync in ContentScript with webextension-polyfill

I have been developing a browser extension in vaniall JS till now. I would like to use vite + Vue moving forward. Upon doing a google search I found this GitHub repository which help with this.
I'm trying to set the user preferences and save them in storage using storage.sync. But when I use browser.storage.sync.get, I get below error
Cannot read properties of undefined (reading 'sync')
How to solve this? What is the correct way to use storage.sync with
webextension-polyfill or #types/webextension-polyfill
Below is the code is have using vanilla js with is woring perfectly
chrome.storage.sync.get({ 'testData': MyTestData }, result => {
console.log(result);
})
Below is the way to use storage.sync using webextension-polyfill
import browser from "webextension-polyfill";
browser.storage.sync.set({ 'testData': MyTestData }, result => {
console.log(result);
})

Docusaurus v2 and GraphQL Playground integration

I'd like to render GraphQL Playground as a React component in one of my pages but it fails due to missing file-loader in webpack. Is there a way to fix this in docs or do I need to create new plugin with new webpack config?
Is it good idea to integrate Playground and Docusaurus at all?
Thanks for your ideas...
A few Docusaurus sites have embedded playgrounds:
Hermes
Uniforms
In your case you will have to write a plugin to extend the webpack config with file-loader.
Not sure if you found a better way but check out: https://www.npmjs.com/package/graphql-playground-react
You can embed this react component directly in your react app - It looks like Apollo also uses the vanilla JS version of this
I just had exactly the same problem. Basically, Docusaurus with a gQL Playground Integration runs fine in local but won't compile due to errors when running yarn build as above.
In the end I found the answer is in Docusaurus, not in building a custom compiler:
I switched from using graphql-react-playground to GraphiQL: package: "graphiql": "^1.8.7"
This moved my error on to a weird one with no references anywhere on the web (rare for me): "no valid fetcher implementation available"
I fixed the above by importing createGraphiQLFetcher from '#graphiql/create-fetcher' to my component
Then the error was around not being able to find a window component, this was an easy one, I followed docusaurus docs here: https://docusaurus.io/docs/docusaurus-core#browseronly and wrapped my component on this page in like this:
import BrowserOnly from '#docusaurus/BrowserOnly';
const Explorer = () => {
const { siteConfig } = useDocusaurusContext();
return (
<BrowserOnly fallback={Loading...}>
{() => {
const GraphEx = GraphExplorer
return
}}
);
}
This now works and builds successfully

What is the best approach to use Laravel localization with Vuejs

I love Laravel localization because it is sample and straightforward, but i dont know how to integrate it whit Vue Components. Any help appreciated.
You can use vue-i18n:
$ npm install vue-i18n
Add it to you Vue app:
import Vue from 'vue'
import App from './App'
import router from './router'
import VueI18n from 'vue-i18n'
Vue.use(VueI18n)
Vue.config.productionTip = false
new Vue({
el: '#app',
router,
template: '<App/>',
components: { App }
})
Just few tips for implementing Laravel resources json-files with Vue when using Vue 3 (as the implementation of vue-i18n is slightly different compared to Vue 2).
I used to use martinlindhe/laravel-vue-i18n-generator, but it has deprecated and hasn't been maintained for a while, so I upgraded my implementation by switching to https://github.com/rmariuzzo/Laravel-JS-Localization.
First I installed Laravel-JS-Localization just as shown at official documentation. This was straightforward. After this I generate JSON-file with:
php artisan lang:js --json resources\js\translations.json
... Please do note that I do not use default directory (/public) as output directory, because I want to bundle translations to Vue-application.
Next I modified my Vue-applications app.js entry point by adding following code before initializing application with createApp(...):
const translations = require('./translations.json');
import { createI18n } from 'vue-i18n'
const i18n = createI18n({
locale: 'fi',
messages: {
fi: translations['fi.strings']
}
})
Remember to use i18n, so that you get translation-methods for templates
const app = createApp(...)
app.use(i18n);
In this example I'm loading Finnish translations, which originally reside at resources/lang/fi.json.
Now I'm able to call $t('Key to translation') within my component template, or this.$t(Key to translation') within logic.

Resources