小编Pet*_*Pan的帖子

如何捕获Web组件中的自定义事件?

为了让 Web 组件相互通信,我使用了自定义事件。让我们想象一下以下情况:

WebComponentA 使用或包含 WebComponentB,如果单击按钮,WebComponentB 会发送 CustomEvent(bubbles:true,composed:true)。如果 WebComponentB 发送此事件,WebComponentA 想要执行某些操作。

我应该如何在 WebComponentB 中调度事件?

window.dispatchEvent(customEvent);
Run Code Online (Sandbox Code Playgroud)

或者

this.shadowRoot.dispatchEvent(customEvent);
Run Code Online (Sandbox Code Playgroud)

我应该如何捕获 WebComponentA 中的事件?

window.addEventListener(custom-event, () => {
);
Run Code Online (Sandbox Code Playgroud)

或者

this.shadowRoot.addEventListener(custom-event, () => {
);
Run Code Online (Sandbox Code Playgroud)

我应该考虑使用其中一种或另一种是否有任何负面影响?

谢谢你!

javascript dom web-component custom-events native-web-component

5
推荐指数
1
解决办法
2627
查看次数

将视图提取到函数或结构中?

我刚开始学习 Swift 和 SwiftUI。我有一个带有视图的结构,其中包含 VStack。在 VStack 中,我调用了四个返回视图的函数。

var body: some View {
    VStack {
        
        self.renderLogo()
        self.renderUserNameTextField()
        self.renderPasswordSecureField()
        self.renderLoginButton()
        
    }
}

func renderLoginButton() -> some View {
    return Button(action: {print("Button clicked")}){
        Text("LOGIN").font(.headline).foregroundColor(.white).padding().frame(width: 220, height: 60).background(Color(red: 0, green: 70/255, blue: 128/255)).cornerRadius(15.0)
    }.padding()
}[...]
Run Code Online (Sandbox Code Playgroud)

我刚刚读到,将视图提取到这样的结构中更为常见:

struct UsernameTextField : View {
@Binding var username: String
var body: some View {
return TextField("Username", text: $username)
            .padding()
            .background(lightGreyColor)
            .cornerRadius(5.0)
            .padding(.bottom, 20)
    }
}
Run Code Online (Sandbox Code Playgroud)

这是为什么?使用结构体代替普通函数有什么优点?

swift swiftui

4
推荐指数
1
解决办法
1101
查看次数

Swift 中的递归枚举

我正在学习 Swift 2(和 C,但也不会太久)不久,我遇到了在递归枚举方面遇到很多困难的地步。

看来,我需要把indirect在之前enum,如果它是递归的。然后我有第一个Int括号之间的情况,因为稍后在 switch 中它返回一个Integer,是吗?

现在是第二种情况的第一个问题Addition。我必须把它放在ArithmeticExpression括号之间。我试着把Int它放在那里,但它给了我一个错误,必须是一个ArithmeticExpression而不是Int. 我的问题是为什么?我无法想象那是什么。为什么我不能在Int那里放两个?

下一个问题ArithmeticExpression又来了。在func solution它进入一个名为 expression 的值中,它的类型ArithmeticExpression是 ,这是正确的吗?其余的,至少现在,完全清楚。如果有人能以简单的方式向我解释这一点,那就太好了。

这是完整的代码:

indirect enum ArithmeticExpression {
    case Number(Int)
    case Addition(ArithmeticExpression, ArithmeticExpression)
}

func solution(expression: ArithmeticExpression) -> Int {
    switch expression {
    case .Number(let value1):
        return value1;
    case . Addition(let value1, let value2):
        return solution(value1)+solution(value2);
    }
}

var ten = ArithmeticExpression.Number(10);
var …
Run Code Online (Sandbox Code Playgroud)

enums func swift swift2

3
推荐指数
2
解决办法
1781
查看次数

x86 汇编器中的除法

我的大学给了我一个练习:

1.在Jasmin中创建一个新文档

2. 使用 AL 寄存器将 9 加到 8。

3. 减去 2。

4. 除以 7。

我的解决方案是:

mov al,9
add al,8
sub al,2
Run Code Online (Sandbox Code Playgroud)

但如何除以 7 呢?我尝试过类似的方法,div al,7但这不起作用。

x86 assembly

2
推荐指数
1
解决办法
4万
查看次数

C中带有几个句子的数组

我怎样才能做到这一点?在Swift 2中,我会做类似的事情:

let test = [
    "I'm a sentence.",
    "Me too.",
    "What am I?",
    "I'm whatever"
]

print(test[1])
Run Code Online (Sandbox Code Playgroud)

我怎么在C中做同样的事情?

我尝试了以下方法:

#include <stdio.h>
#include <string.h>

int main()
{
 const char strings[] = {
        "I'm a sentence.",
        "Me too.",
        "What am I?",
        "I'm whatever"
  };
  printf("%s", strings[1]);
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

c arrays swift swift2

1
推荐指数
1
解决办法
688
查看次数

最高(现有)半精度IEEE 754

为什么0 11110 1111111111不是0 11111 1111111111最高半精度数?

floating-point binary ieee ieee-754

1
推荐指数
1
解决办法
694
查看次数

获得C中的"最高"字符

我试图写一个程序,它获得了一个人进入的最高性格.我制作了一个程序,它可以获得最高数量的工作,没有任何问题但是字符不起作用.这是我的代码:

#include <stdio.h>
#include <string.h>

int main(int argc, const char * argv[]) {
    char characters[5];
    char highest = "a";

    printf("Please enter five characters: \n");
    for (int i = 0; i <= 4; i+=1) {
        scanf("%c", characters[i]);
    }

    printf("These are the characters you entered: ");

    for (int i = 0; i <= 4; i+=1) {
        printf("%c  ", characters[i]);
    }

    for (int i = 0; i <= 4; i+=1) {
        if (characters[i] > highest) {
            highest = characters[i];
        }
    }

    printf("\nThe highest …
Run Code Online (Sandbox Code Playgroud)

c printf for-loop

-1
推荐指数
1
解决办法
89
查看次数