Skip to content

Use typescriptin vue, when overide one method in child component, in this method cannot call parent method? #8451

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

Closed
hesi726 opened this issue Jul 4, 2018 · 1 comment

Comments

@hesi726
Copy link

hesi726 commented Jul 4, 2018

It only happened in Vue. (In typescript it works).

I use vue-cli to create a project and accord the
vuejs/vue-cli#1104
TypeStrong/fork-ts-checker-webpack-plugin#111
to split the .ts, .vue file in tow file.
and i write the follows code.

BaseComponent.ts

`import { Component, Prop, Vue } from 'vue-property-decorator';
@component({})
export default class BaseComponent extends Vue {
/**
* value changed
*/
public on_value_changed() {
this.before_value_changed();
}

/**
 * nothing to do but log one string
 */
public before_value_changed() {
    console.log('before_value_changed');
}

}
`
HelloWorld.ts

`import { Component, Prop, Vue } from 'vue-property-decorator';
import BaseComponent from '@/components/BaseComponent';

@component({})
export default class HelloWorld extends BaseComponent {
// @prop() private msg!: string;
private count: number = 12;

/**
 * override the before_value_changed function
 */
public before_value_changed() {        
    super.before_value_changed();  // **// will error**
    this.count++;
}

}
`
HelloWorld.vue

<template> <div class="hello"> <button @click='on_value_changed'>Test</button> {{count}} </div> </template>

when in the before_value_changed in HelloWorld.ts , use super.before_value_changed call the before_value_changed method of BaseComponent, it will product error.

HelloWorld.ts?b79b:13 Uncaught TypeError: Cannot read property 'call' of undefined

in typescript, it work fine.
http://www.typescriptlang.org/play/index.html:

`class BaseComponent {
public before_value_changed() {
}
public on_value_changed() {
this.before_value_changed();
return "Hello, ";
}
}

class HelloWorld extends BaseComponent {
public before_value_changed() {
console.log('beforeGreet');
super.before_value_changed();
}
}
let hello = new HelloWorld();

let button = document.createElement('button');
button.textContent = "Say Hello";
button.onclick = function() {
alert(hello.on_value_changed());
}
document.body.appendChild(button);`

@vue-bot
Copy link
Contributor

vue-bot commented Jul 4, 2018

Hello, your issue has been closed because it does not conform to our issue requirements. In order to ensure every issue provides the necessary information for us to investigate, we require the use of the Issue Helper when creating new issues. Thank you!

@vue-bot vue-bot closed this as completed Jul 4, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants