我的 angular ui 路由器将状态/视图更改为:
$stateProvider
.state({
name: 'home',
url: '/',
template: '<home-view></home-view>',
})
Run Code Online (Sandbox Code Playgroud)
有没有办法将绑定参数传递给 UI 路由器中的 < home-view > ?
例如
<home-view my-data="dataObject">
Run Code Online (Sandbox Code Playgroud)
或者
<home-view my-data="myService.getMyData()">
Run Code Online (Sandbox Code Playgroud)
my-view 的控制器位于包含实际模板和指令的自己的文件中。
javascript angularjs angular-template angular-ui-router es6-class
我写了这个类并为它设置了一个数组属性.然后,我想在此数组中添加一个项目.
然而,当我尝试这样做,我得到的错误"未捕获TypeError:无法读取属性push的undefined".
这不可能吗?
class test {
constructor() {
this.myArray = [];
}
myMethod() {
this.myArray.push("ok");
}
};
console.log(test.prototype.myMethod());Run Code Online (Sandbox Code Playgroud)
这是clock.js我的React应用的一个文件()
class Clock extends Component { ...
...}
export default Clock;
Run Code Online (Sandbox Code Playgroud)
这是我要导入Clock组件的另一个地方
import Clock_1 from './clock/clock';
...
<Route exact path="/clock" component={Clock_1} />
Run Code Online (Sandbox Code Playgroud)
如您所见,我以Clock的名称导出了它,并以Clock_1的名称导入了它,但它仍在编译并成功运行。为什么?
PS: Apologies beforehand if this question sounds lame/ too simple/ stupid. I'm a beginner.
所以我有一个类,其中一个函数依赖于另一个函数。该类与模块一起导出。根据我能找到的任何内容,我应该能够使用“this”,但这会引发错误。
例子:
class Test{
test(){
console.log('hello');
}
dependentMethod(){
this.test();
}
}
module.exports = Test;
Run Code Online (Sandbox Code Playgroud)
然而,这会在节点中引发这些错误:
(node:69278) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): TypeError: Cannot read property 'test' of undefined
(node:69278) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
(node:69278) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): TypeError: Cannot read property 'test' of undefined
Run Code Online (Sandbox Code Playgroud)
如果我把这个函数放在类之外,它会工作得很好。谁能解释为什么会失败?:)
编辑:
这是 server.js 中使用该类的代码(针对示例进行了简化):
const test = …Run Code Online (Sandbox Code Playgroud) 我想使用 super 的构造函数将子类实例传递给超类,但出现此错误
super(this);
Run Code Online (Sandbox Code Playgroud)
这在超类构造函数调用之前是不允许的
为什么我收到这个错误,我该如何解决这个问题
class Parent
{
constructor(child)
{
this.child = child;
}
//...somewhere in code
//child.doSomething();
}
class Child extends Parent
{
constructor()
{
super(this); // <==== the error here
}
doSomething = () =>
{
//...
}
}
Run Code Online (Sandbox Code Playgroud) class SomeClass {
someMethod() {
// some code
}
someMoreMethod() {
// some more code
}
}
var someInstance = new someClass();
Run Code Online (Sandbox Code Playgroud)
我们知道,在上面的代码,方法someMethod和someMoreMethod将得到重视的原型someInstance对象.但是,如果我们想要将一些属性(而不是方法)附加到原型上,该怎么办?我尝试执行以下操作,但它会抛出错误:
class SomeClass {
someProperty = "Some Value";
someMethod() {
// some code
}
someMoreMethod() {
// some more code
}
}
Run Code Online (Sandbox Code Playgroud) 我尝试了很多东西,但是都没有用。我想知道这是否不可能吗?我知道“绑定”的“正常”方式,但是箭头功能更具可读性,我更喜欢使用它们。
为了更好地理解我的问题,我制作了此示例代码来尽可能全面地说明问题。
class MyClass_XY {
constructor(zID) {
let ref = document.getElementById(zID);
this.name = zID;
this.Info = ref.querySelector('span');
this._Bt_Plus = ref.querySelector('.plus');
this._bt_Stop = ref.querySelector('.stop');
this.Nbre = 0;
// this.stop = false; // I don't whant this, because this is a small sample code of something more complex
this._Bt_Plus.onclick = e => this._f_Bt_Plus_click(e);
this._bt_Stop.onclick = e => this._f_Bt_Stop_click(e);
/*
this.fct_Ref = null;
this._Bt_Plus.addEventListener('click', this.fct_Ref = this._f_Bt_Plus_click.bind(this) , false );
*/
}
_f_Bt_Plus_click(e) {
e.stopPropagation();
console.log(e.target.innerText);
this.Nbre++,
this.Info.innerText = this.Nbre.toString();
}
_f_Bt_Stop_click(e) { …Run Code Online (Sandbox Code Playgroud)因此,当他们创建 es6 类时,他们只是默认将所有内容设为公开,这对我来说有点奇怪,但我还是照做了,因为我仍然使用旧的 es5 样式基于作用域的类。
无论如何,几年过去了,我们在班级中加入了私人成员,这看起来很棒,但是你看一下 Synax:
somePublicVar = 10;
#somePrivateVar = 20;
Run Code Online (Sandbox Code Playgroud)
正如你所看到的,我们现在必须用哈希/磅符号作为私有内容的前缀,考虑到 JS 有public和private保留供将来使用的关键字,这似乎是一个非常奇怪的选择,所以现在我们想区分 public 和 private 为什么不是现在做。
public somePublicVar = 10;
private somePrivateVar = 20;
Run Code Online (Sandbox Code Playgroud)
所以,我相信有为什么,但我在努力找到一个技术原因,因为它现在似乎是把那些最完美的时间public,并private从保留“使用”,使得它多一点明确,明显为给定成员的访问修饰符。
我最近开始学习Classesin javascript,在阅读一些非常有趣的内容时,我想到了尝试自己的一些想法。
如果你有一个父类Parent中,你有一个method的logSomething```` and a child class of子, with which you do类儿童家长延伸, how can you then execute the inherited method from the parent class,logSomething```,子类的里面?
如果在Child类的内部定义方法并添加this.logSomething()到该方法,则无论何时调用子类中的方法,继承的logSomething函数的确会运行,但除此之外,我还没有找到logSomething直接执行该方法的方法。儿童班。
我尝试过this.logSomething(),我尝试过将其添加到对象,自执行(IIFE)函数以及我能做的所有事情,但没有任何结果。
class Parent {
constructor() {}
logSomething() {
console.log('I am logging something')
}
}
class Child extends Paren {
logSomething() // This does not work
}
Run Code Online (Sandbox Code Playgroud)
当前这样做是行不通的,如果抛出一个错误的事实,那就是您试图定义一个函数的事实。
我知道应该可以以某种方式实现,如果我不误会React使用类似的life-cycle methods …
如果这已经被问到,请告诉我。如何在每个实例上增加类变量?假设我有以下 Key 类,我想在创建实例时增加 key 变量,我尝试过:
class Key{
key = 1
constructor(){
this.key = key
Key.key++
}
print_key(){
console.log(this.key)
}
}
Run Code Online (Sandbox Code Playgroud)
然后我打印 make 几个实例:
key1 = new Key()
key2 = new Key()
key3 = new Key()
key1.print_key()
key2.print_key()
key3.print_key()
Run Code Online (Sandbox Code Playgroud)
想要的结果是:
1
2
3
Run Code Online (Sandbox Code Playgroud)
上面的代码不起作用,我找不到具体的答案,或者某些答案似乎对我不起作用。
我希望能够从类中获取实例化类的实例的名称.
例:
class MyClass {
getInstanceName() {
// code that figures out the name given to the instance
// instantiating this class
}
}
var inst = new MyClass();
console.log(inst.getInstanceName()); // should log "inst"
Run Code Online (Sandbox Code Playgroud)
我想让它从类中发出一个带有实例名称的事件.我在node.js中这样做
编辑:这个问题不同于如何扩展一个类而不必在 ES6 中使用超级?- 虽然答案是相关的,但这显然是一个不同的问题。它涉及到一个特定的错误,并且两大类参与Person和CreationEvent实际上并不相互继承。
我有两个 ES6 类,Person并且CreationEvent(CreationEvent继承自Event)。我希望new CreationEvent在我制作 a时制作 a new Person(因为这CreationEvent是个人帐户历史中事件的一部分)。
运行new CreationEvent()它自己的工作正常。但是我不能运行new Person()。
即使使用简化版本的代码仍然失败:
class Event {
constructor() {
this.time = Date.now()
this.tags = []
}
}
class CreationEvent extends Event {
constructor() {
this.description = "Created"
}
}
class Person {
constructor(givenName, familyName, email) {
var creationEvent = new CreationEvent()
}
} …Run Code Online (Sandbox Code Playgroud) es6-class ×12
javascript ×11
ecmascript-6 ×7
node.js ×2
angularjs ×1
class ×1
node-modules ×1
oop ×1
prototype ×1
react-redux ×1
reactjs ×1