-
Notifications
You must be signed in to change notification settings - Fork 8.1k
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
[zh-cn] refactor Using_custom_elements #17038
Conversation
The existing translation is outdated and contains incorrect translation, so I translated the latest English document, which significantly reduce misleading to reader.
Dear reviewer, some term has no standard translation so I keep them as they are (mainly: |
files/zh-cn/web/api/web_components/using_custom_elements/index.md
Outdated
Show resolved
Hide resolved
files/zh-cn/web/api/web_components/using_custom_elements/index.md
Outdated
Show resolved
Hide resolved
files/zh-cn/web/api/web_components/using_custom_elements/index.md
Outdated
Show resolved
Hide resolved
files/zh-cn/web/api/web_components/using_custom_elements/index.md
Outdated
Show resolved
Hide resolved
files/zh-cn/web/api/web_components/using_custom_elements/index.md
Outdated
Show resolved
Hide resolved
files/zh-cn/web/api/web_components/using_custom_elements/index.md
Outdated
Show resolved
Hide resolved
files/zh-cn/web/api/web_components/using_custom_elements/index.md
Outdated
Show resolved
Hide resolved
files/zh-cn/web/api/web_components/using_custom_elements/index.md
Outdated
Show resolved
Hide resolved
files/zh-cn/web/api/web_components/using_custom_elements/index.md
Outdated
Show resolved
Hide resolved
files/zh-cn/web/api/web_components/using_custom_elements/index.md
Outdated
Show resolved
Hide resolved
files/zh-cn/web/api/web_components/using_custom_elements/index.md
Outdated
Show resolved
Hide resolved
files/zh-cn/web/api/web_components/using_custom_elements/index.md
Outdated
Show resolved
Hide resolved
files/zh-cn/web/api/web_components/using_custom_elements/index.md
Outdated
Show resolved
Hide resolved
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
files/zh-cn/web/api/web_components/using_custom_elements/index.md
Outdated
Show resolved
Hide resolved
files/zh-cn/web/api/web_components/using_custom_elements/index.md
Outdated
Show resolved
Hide resolved
files/zh-cn/web/api/web_components/using_custom_elements/index.md
Outdated
Show resolved
Hide resolved
files/zh-cn/web/api/web_components/using_custom_elements/index.md
Outdated
Show resolved
Hide resolved
files/zh-cn/web/api/web_components/using_custom_elements/index.md
Outdated
Show resolved
Hide resolved
files/zh-cn/web/api/web_components/using_custom_elements/index.md
Outdated
Show resolved
Hide resolved
files/zh-cn/web/api/web_components/using_custom_elements/index.md
Outdated
Show resolved
Hide resolved
files/zh-cn/web/api/web_components/using_custom_elements/index.md
Outdated
Show resolved
Hide resolved
files/zh-cn/web/api/web_components/using_custom_elements/index.md
Outdated
Show resolved
Hide resolved
Co-authored-by: skyclouds2001 <[email protected]>
Co-authored-by: skyclouds2001 <[email protected]>
files/zh-cn/web/api/web_components/using_custom_elements/index.md
Outdated
Show resolved
Hide resolved
|
||
{{domxref("CustomElementRegistry.define()")}} 方法用来注册一个 custom element,该方法接受以下参数: | ||
- **Customized built-in elements** 继承自标准的 HTML 元素,例如 {{domxref("HTMLImageElement")}} 或 {{domxref("HTMLParagraphElement")}}。它们的实现定义了标准元素的行为。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- **Customized built-in elements** 继承自标准的 HTML 元素,例如 {{domxref("HTMLImageElement")}} 或 {{domxref("HTMLParagraphElement")}}。它们的实现定义了标准元素的行为。 | |
- **自定义内置元素**继承自标准的 HTML 元素,例如 {{domxref("HTMLImageElement")}} 或 {{domxref("HTMLParagraphElement")}}。它们的实现定义了标准元素的行为。 |
|
||
{{domxref("CustomElementRegistry.define()")}} 方法用来注册一个 custom element,该方法接受以下参数: | ||
- **Customized built-in elements** 继承自标准的 HTML 元素,例如 {{domxref("HTMLImageElement")}} 或 {{domxref("HTMLParagraphElement")}}。它们的实现定义了标准元素的行为。 | ||
- **Autonomous custom elements** 继承自 HTML 元素基类 {{domxref("HTMLElement")}}。你必须从头开始实现它们的行为。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Candidate translation
/cc @jasonren0403 what's your opinion?
- **Autonomous custom elements** 继承自 HTML 元素基类 {{domxref("HTMLElement")}}。你必须从头开始实现它们的行为。 | |
- **独立自定义元素**继承自 HTML 元素基类 {{domxref("HTMLElement")}}。你必须从头开始实现它们的行为。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I prefer using this too 👍
|
||
作为示例,我们可以像这样定义一个叫做 [word-count](https://mdn.github.io/web-components-examples/word-count-web-component/) 的 custom element: | ||
自定义元素作为一个[类](/docs/Web/JavaScript/Reference/Classes)来实现,该类可以扩展 {{domxref("HTMLElement")}}(对于 autonomous elements)或者你想要定制的接口(对于 customized built-in elements)。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
自定义元素作为一个[类](/docs/Web/JavaScript/Reference/Classes)来实现,该类可以扩展 {{domxref("HTMLElement")}}(对于 autonomous elements)或者你想要定制的接口(对于 customized built-in elements)。 | |
自定义元素作为一个[类](/docs/Web/JavaScript/Reference/Classes)来实现,该类可以扩展 {{domxref("HTMLElement")}}(即独立元素的情况)或者你想要定制的接口(即自定义内置元素的情况)。 |
files/zh-cn/web/api/web_components/using_custom_elements/index.md
Outdated
Show resolved
Hide resolved
files/zh-cn/web/api/web_components/using_custom_elements/index.md
Outdated
Show resolved
Hide resolved
files/zh-cn/web/api/web_components/using_custom_elements/index.md
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Valid update. Please check those comments before we can merge this :)
files/zh-cn/web/api/web_components/using_custom_elements/index.md
Outdated
Show resolved
Hide resolved
files/zh-cn/web/api/web_components/using_custom_elements/index.md
Outdated
Show resolved
Hide resolved
console.log("Custom element added to page."); | ||
} | ||
|
||
... | ||
disconnectedCallback() { | ||
console.log("Custom element removed from page."); | ||
} | ||
|
||
adoptedCallback() { | ||
console.log("Custom element moved to new page."); | ||
} | ||
|
||
attributeChangedCallback(name, oldValue, newValue) { | ||
console.log(`Attribute ${name} has changed.`); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could you translate those texts. This example is not a part of live examples (which are not localized). So it's better to localize them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
got it.
... | ||
attributeChangedCallback(name, oldValue, newValue) { | ||
console.log( | ||
`Attribute ${name} has changed from ${oldValue} to ${newValue}.`, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
translate this
``` | ||
|
||
上述代码片段中,类的构造函数{{jsxref("Classes.constructor","constructor")}}总是先调用[`super()`](/zh-CN/docs/Web/JavaScript/Reference/Operators/super)来建立正确的原型链继承关系。 | ||
请注意,如果元素的 `HTML` 声明包含一个被观察的属性,那么在元素的声明首次解析时,`attributeChangedCallback()` 将被调用,即在属性被初始化后。因此,在以下示例中,即使属性再也没有被更改,当 DOM 被解析时,`attributeChangedCallback()` 也会被调用:(译注:即每个直接在 HTML 中声明的且被观察的属性会立即触发 `attributeChangedCallback()` 一次) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's remove all the translation note, if the body is not will written, please update the original English content.
请注意,如果元素的 `HTML` 声明包含一个被观察的属性,那么在元素的声明首次解析时,`attributeChangedCallback()` 将被调用,即在属性被初始化后。因此,在以下示例中,即使属性再也没有被更改,当 DOM 被解析时,`attributeChangedCallback()` 也会被调用:(译注:即每个直接在 HTML 中声明的且被观察的属性会立即触发 `attributeChangedCallback()` 一次) | |
请注意,如果元素的 `HTML` 声明包含一个被观察的属性,那么在元素的声明首次解析时,`attributeChangedCallback()` 将被调用,即在属性被初始化后。因此,在以下示例中,即使属性再也没有被更改,当 DOM 被解析时,`attributeChangedCallback()` 也会被调用: |
|
||
## 示例 | ||
|
||
在本指南的其余部分,我们将看一些示例自定义元素。你可以在 [web-components-examples](https://github.com/mdn/web-components-examples) 存储库中找到所有这些示例的源代码,以及更多示例,并且你可以在 <https://mdn.github.io/web-components-examples/> 上实时查看它们。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We use "仓库" rather than "存储库" (align with other documents)
在本指南的其余部分,我们将看一些示例自定义元素。你可以在 [web-components-examples](https://github.com/mdn/web-components-examples) 存储库中找到所有这些示例的源代码,以及更多示例,并且你可以在 <https://mdn.github.io/web-components-examples/> 上实时查看它们。 | |
在本指南的其余部分,我们将看一些示例自定义元素。你可以在 [web-components-examples](https://github.com/mdn/web-components-examples) 仓库中找到所有这些示例的源代码,以及更多示例,并且你可以在 <https://mdn.github.io/web-components-examples/> 上实时查看它们。 |
- `disconnectedCallback`:当 custom element 从文档 DOM 中删除时,被调用。 | ||
- `adoptedCallback`:当 custom element 被移动到新的文档时,被调用。 | ||
- `attributeChangedCallback`: 当 custom element 增加、删除、修改自身属性时,被调用。 | ||
到目前为止,我们只看到了一个生命周期回调的实际应用:`connectedCallback()`。在最后一个例子中,`<custom-square>`,我们将看到一些其他的回调。`<custom-square>` 独立自定义元素绘制一个正方形,其大小和颜色由两个名为 `"size"` 和 `"color"` 的属性确定。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
到目前为止,我们只看到了一个生命周期回调的实际应用:`connectedCallback()`。在最后一个例子中,`<custom-square>`,我们将看到一些其他的回调。`<custom-square>` 独立自定义元素绘制一个正方形,其大小和颜色由两个名为 `"size"` 和 `"color"` 的属性确定。 | |
到目前为止,我们只看到了一个生命周期回调的实际应用:`connectedCallback()`。在最后一个示例——`<custom-square>`——中,我们将看到一些其他的回调。`<custom-square>` 独立自定义元素绘制一个正方形,其大小和颜色由两个名为 `"size"` 和 `"color"` 的属性确定。 |
Co-authored-by: A1lo <[email protected]>
files/zh-cn/web/api/web_components/using_custom_elements/index.md
Outdated
Show resolved
Hide resolved
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
May be we need to update translation for Using_shadow_DOM one day. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @YieldRay, @skyclouds2001!
Feel free to update it whenever you have time. |
The existing translation is outdated and contains incorrect translation, so I translated the latest English document, which significantly reduce misleading to reader.
Additional details
Source:
https://github.com/mdn/content/blob/main/files/en-us/web/api/web_components/using_custom_elements/index.md?plain=1