New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Please add an example of using an array of string objects to initialize a component. #137
Comments
You can just declare import Vue from 'vue'
import { Component } from 'vue-property-decorator';
interface GeneralMenuItem {
routename: string;
menuiconclass:string;
menuoption:string
}
@Component({
props: {
pmenuitems: Array
}
})
export default class GeneralMenuComponent extends Vue {
pmenuitems: GeneralMenuItem[];
menuitems: GeneralMenuItem[];
mounted() {
this.menuitems = pmenuitems;
} |
Thanks for the reply but it just doesn't work for me. I see in their fetchdata.ts they use " (response => response.json() as Promise<WeatherForecast[]>)" My main issue is not understanding how to declare or cast the incoming array property to the required type ( in my case GeneralMenuItem ) in Typescript, whether using JSON or not. Any help would be appreciated. |
ANSWER: for anyone having the same issue, one answer is to export the interface, i.e. the type you to want use elsewhere like this: // In generalmenu.ts
export interface GeneralMenuItem{
routename: string;
menuiconclass:string;
menuoption: string;
} Then you can use it where you want to pass the property to your component by importing this interface like this: // In file app.ts
import { GeneralMenuItem } from 'your-component-location'
import GeneralMenuComponent from 'your-component-location'
// I'm using visual studio and it actually helps out with this!
//Then you can use it as expected:
.
.
.
export default class AppComponent extends Vue {
// Now you have type checking available at this point and no more compilation errors
gmenuitems: GeneralMenuItem[] = [{...},{...}, etc];
} I hope this helps. Might not be the ideal solution but definitely works for me. |
I"m new to this game so please reserve the flames and ire for another day.
I'm trying to initialize a menu component with an array consisting of objects containing the following:
routeName : string,
menuIconClass : string,
menuOption : string
Component skeleton:
The text was updated successfully, but these errors were encountered: