Incorrect/misleading example use-case for `ifDefined` directive

Đang mở
#1,400 1 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
52/100
Loại issue
Tài liệu
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Đình trệ
Công nghệ
typescript
Lĩnh vực
documentation

Hướng nghiên cứu

Bắt đầu với phần về directive ifDefined trong tài liệu Lit được liên kết và xem lại hướng dẫn được trích dẫn cùng ví dụ về hình ảnh. Cập nhật ví dụ hoặc phần giải thích để không trình bày img src chưa hoàn chỉnh như một cách thực hành được khuyến nghị; phần này hoàn tất khi trường hợp sử dụng được ghi lại là chính xác và rõ ràng.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

Hi! In the docs for the ifDefined directive, we read the following:

This is especially useful for setting URL attributes, when the attribute should not be set if required parts of the URL are not defined, to prevent 404's.

and there's the following code example:

@customElement('my-element')
class MyElement extends LitElement {

  @property()
  filename: string | undefined = undefined;

  @property()
  size: string | undefined = undefined;

  render() {
    // src attribute not rendered if either size or filename are undefined
    return html`<img src="/images/${ifDefined(this.size)}/${ifDefined(this.filename)}">`;
  }
}

The example clearly shows how it works, however the src is a mandatory attribute of the img element, and in such a case, I think the better way of doing things would be to not render the img at all.

Please don't get me wrong – I'm of course aware that this is just an example, but if such a code snippet is presented in a documentation of a library, it could be considered by many people as a good/common practice.

I wasn't able to find out a good alternative to that in a reasonable timespan, so I'm just leaving this issue as a note or a small issue report.

Ngôn ngữ chính
TypeScript
Star
140
Fork
229
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của lit/lit.dev

Tất cả issue của lit/lit.dev

Issue tương tự

Thêm issue về TypeScript

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.