question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot pass vue-i18n object into web-component

See original GitHub issue

I have a component that uses vue-i18n and works as a normal vue component.

When creating web component I had TypeError: this is undefined error with typescript, then created the component with this approach:

const layout = new ModuleLayout().$options;
Vue.customElement('my-layout', layout);

And this is my i18n object:

import messages from '@/locales/i18n.json';

export const i18n = new VueI18n({
  locale: process.env.VUE_APP_I18N_LOCALE || 'en',
  fallbackLocale: process.env.VUE_APP_I18N_FALLBACK_LOCALE || 'en',
  messages: messages
})

For adding i18n I tried these approaches but none works:

const layout = new ModuleLayout({ i18n }).$options;

Vue.customElement('my-layout', layout);
const layout = new ModuleLayout().$options;
layout.i18n = i18n;

Vue.customElement('my-layout', layout);

In both ways I have these errors:

TypeError: "this.$i18n is undefined"
TypeError: "i18n is undefined"

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
rostamianicommented, Jul 8, 2020

it works , but with a typescript error:

 Property 'options' does not exist on type 'Function'.

image

Update: While serving, after recompiling this.$i18n is undefined error appears again.

1reaction
karol-fcommented, Jul 7, 2020

Hi, it seems to work like this. Please let me know if it will work for You - https://codesandbox.io/s/vue-template-72o5z?file=/src/main.js

import Vue from "vue";
import vueCustomElement from "vue-custom-element";
import VueI18n from "vue-i18n";
import Test from "./components/Test.vue";

Vue.use(vueCustomElement);
Vue.use(VueI18n);

Vue.config.productionTip = false;

const i18n = new VueI18n({
  locale: "en",
  messages: {
    en: {
      message: {
        hello: "hello world"
      }
    }
  }
});
Test.prototype.constructor.options.i18n = i18n;

Vue.customElement("test-component", Test.prototype.constructor.options);
Read more comments on GitHub >

github_iconTop Results From Across the Web

Vuejs: Can't pass object to web component using v-bind
v-bind assumes an attribute if it can't determine that the target is a property, which would be the case for Stencil components.
Read more >
Web components - Vue I18n
However, the provided Web Components cannot be inserted directly into HTML. You need to prepare the following Web Components to host the i18n...
Read more >
How to integrate Web Components using Lit in Angular
In this case, we'll install it using npm. npm install --save @webcomponents/webcomponentsjs. Then you can ...
Read more >
Deep dive into WebComponents - ITNEXT
This is a size comparison between the most common web frameworks: Vanilla Custom Elements → ready to use; LitElement → ~5kb; Vue.js →...
Read more >
Web Components integration guide - Adyen Docs
You cannot set the session expiry to more than 24 hours after session ... sessionData : the payment session data you need to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found