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.

@Inject decorate usage with provide

See original GitHub issue

Hi,

As a follow-up question from #11 , I wonder if this is correct approach or not.

Foo.ts

class Foo {
  public bar: string = "Hello World";
}

export default Foo;

ParentComponent.vue:

<template>
  <child-component></child-component>
</template>

<script language="ts">
import Vue from "vue";
import { Component } from "vue-property-decorator";
import Foo from "./Foo";
import ChildComponent from "./ChildComponent.vue";

let foo = new Foo();

@Component({
  name: "Parent",
  components: {
    childComponent: ChildComponent
  },
  provide: foo
})
export default class ParentComponent extends Vue {
}
</script>

ChildComponent.vue:

<template>
  <p>{{ foo.bar }}</p>
</template>

<script language="ts">
import Vue from "vue";
import { Component, Inject } from "vue-property-decorator";
import Foo from "./Foo";

@Component({
  name: "Child"
})
export default class ChildComponent extends Vue {
  @Inject(Symbol("Foo"))
  public foo: Foo;

  created(): void {
    console.log(this.foo.bar);
  }
}
</script>

When I ran this code, this.foo in the ChildComponent is undefined. In other words, the Foo instance has not been properly injected from ParentComponent. Could you confirm that I used both provide/inject pair properly?

Cheers,

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
RockyNiucommented, Mar 31, 2017
0reactions
kaorun343commented, Apr 12, 2017

@pensacola1989

Hi.

This is not a @Inject specific problem.

When using vue-class-component, please set this.foo at created hook (or mounted). Then it’ll work well.

In addition, this.axios = axios is not a good code because when assigning an object at constructor (this is equivalent to assign an object in data option when writing in plain JavaScript), the property becomes reactive property. So, in my humble opinion, I recommend you to avoid assigning it at constructor option. As well as this.foo, it’ll be better to assign it at created hook than you do.

@Component()
export class ListComponent extends Vue {
  items: UserResponse[] = []
  private url = 'your url'
  protected axios: typeof axios
  
  @Inject(foo) foo: Foo

  created() {
    console.log(typeof this.foo) // 'object'
    this.axios = axios
  }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Dependency Injection using Decorators | by Chidume Nnamdi
Decorators are an experimental feature in ES6 that is designed to modify class, methods, parameters at runtime. It provides a way of adding...
Read more >
Simple Injector decoration with additional dependency
There are multiple ways to do this in Simple Injector. First of all, you can let your decorator's constructor depend on the DecoratorContext...
Read more >
How to Use the @Injectable Decorator in Angular | Pluralsight
The @Injectable decorator together with the 'providedIn' option means the service should not be added within the providers' array of a module.
Read more >
Angular & Dependency Injection: tricks with Decorators
They're decorators which we can apply to our dependencies (the ones we place in the constructor of our classes). They're not used too...
Read more >
Adding decorated classes to the ASP.NET Core DI container ...
In my last post, I described how you can use Scrutor to add assembly scanning to the ASP.NET Core built-in DI container, without...
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