@wsxjs/wsx-router 0.0.19 → 0.0.21

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wsxjs/wsx-router",
3
- "version": "0.0.19",
3
+ "version": "0.0.21",
4
4
  "description": "WSX Router - Native History API-based routing for WSXJS",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",
@@ -18,8 +18,8 @@
18
18
  "!**/test"
19
19
  ],
20
20
  "dependencies": {
21
- "@wsxjs/wsx-core": "0.0.19",
22
- "@wsxjs/wsx-logger": "0.0.19"
21
+ "@wsxjs/wsx-logger": "0.0.21",
22
+ "@wsxjs/wsx-core": "0.0.21"
23
23
  },
24
24
  "devDependencies": {
25
25
  "@typescript-eslint/eslint-plugin": "^8.37.0",
@@ -31,8 +31,8 @@
31
31
  "typescript": "^5.0.0",
32
32
  "vite": "^5.4.19",
33
33
  "vitest": "^2.1.8",
34
- "@wsxjs/eslint-plugin-wsx": "0.0.19",
35
- "@wsxjs/wsx-vite-plugin": "0.0.19"
34
+ "@wsxjs/wsx-vite-plugin": "0.0.21",
35
+ "@wsxjs/eslint-plugin-wsx": "0.0.21"
36
36
  },
37
37
  "keywords": [
38
38
  "wsx",
package/src/WsxLink.wsx CHANGED
@@ -25,9 +25,15 @@ export default class WsxLink extends WebComponent {
25
25
  static observedAttributes = ["to", "replace", "active-class", "exact"];
26
26
 
27
27
  @state private to: string = "";
28
- @state private replace: boolean = false;
28
+ get replace(): boolean {
29
+ return this.hasAttribute("replace");
30
+ }
31
+
32
+ get exact(): boolean {
33
+ return this.hasAttribute("exact");
34
+ }
35
+
29
36
  @state private activeClass: string = "active";
30
- @state private exact: boolean = false;
31
37
 
32
38
  render() {
33
39
  return (
@@ -39,10 +45,8 @@ export default class WsxLink extends WebComponent {
39
45
 
40
46
  protected onConnected() {
41
47
  // Initialize attributes from HTML when connected
42
- this.to = this.getAttribute("to") || "";
43
- this.replace = this.hasAttribute("replace");
44
- this.activeClass = this.getAttribute("active-class") || "active";
45
- this.exact = this.hasAttribute("exact");
48
+ this.to = this.to || "";
49
+ this.activeClass = this.activeClass || "active";
46
50
 
47
51
  // Update the href of the rendered link
48
52
  const link = this.shadowRoot.querySelector(".wsx-link") as HTMLAnchorElement;
package/src/WsxView.wsx CHANGED
@@ -1,7 +1,6 @@
1
1
  /** @jsxImportSource @wsxjs/wsx-core */
2
- import { LightComponent, autoRegister } from "@wsxjs/wsx-core";
2
+ import { LightComponent, state, autoRegister } from "@wsxjs/wsx-core";
3
3
  import { createLogger } from "@wsxjs/wsx-logger";
4
- import styles from "./WsxView.css?inline";
5
4
 
6
5
  const logger = createLogger("WsxView");
7
6
 
@@ -20,20 +19,26 @@ const logger = createLogger("WsxView");
20
19
  */
21
20
  @autoRegister({ tagName: "wsx-view" })
22
21
  export default class WsxView extends LightComponent {
23
- static observedAttributes = ["route", "component", "params"];
22
+ get component(): string | null {
23
+ return this._component;
24
+ }
25
+ set component(value: string | null) {
26
+ this._component = value || "";
27
+ // 延迟加载,避免在 attributeChangedCallback 中同步执行
28
+ if (this._component) {
29
+ requestAnimationFrame(() => {
30
+ if (this.connected && !this.componentInstance && !this.isLoading) {
31
+ this.loadComponent(this._component || "");
32
+ }
33
+ });
34
+ }
35
+ }
36
+ private _component: string | null = "";
24
37
 
25
- private component: string | null = null;
26
- private params: Record<string, string> = {};
38
+ @state private params: Record<string, string> = {};
27
39
  private componentInstance: HTMLElement | null = null;
28
40
  private isLoading: boolean = false; // 防止重复加载的标志
29
41
 
30
- constructor() {
31
- super({
32
- styles,
33
- styleName: "wsx-view",
34
- });
35
- }
36
-
37
42
  render() {
38
43
  return (
39
44
  <div class="route-view">
@@ -50,54 +55,17 @@ export default class WsxView extends LightComponent {
50
55
  return;
51
56
  }
52
57
 
53
- const componentName = this.getAttribute("component");
54
- if (componentName) {
55
- this.loadComponent(componentName);
58
+ if (this.component) {
59
+ this.loadComponent(this.component);
56
60
  }
57
61
  });
58
62
  }
59
63
 
60
- protected onAttributeChanged(name: string, _oldValue: string, newValue: string) {
61
- // 只在组件已连接时处理属性变化
62
- if (!this.connected) {
63
- return;
64
- }
65
-
66
- if (name === "component" && newValue && !this.componentInstance && !this.isLoading) {
67
- // 延迟加载,避免在 attributeChangedCallback 中同步执行
68
- requestAnimationFrame(() => {
69
- if (this.connected && !this.componentInstance && !this.isLoading) {
70
- this.loadComponent(newValue);
71
- }
72
- });
73
- } else if (name === "params" && this.componentInstance) {
74
- // 更新组件参数
75
- try {
76
- this.params = JSON.parse(newValue);
77
- logger.debug(
78
- `WsxView: params attribute changed, setting on component:`,
79
- this.params
80
- );
81
- Object.entries(this.params).forEach(([key, value]) => {
82
- this.componentInstance!.setAttribute(key, value);
83
- });
84
- logger.debug(`WsxView: params set on component ${this.componentInstance.tagName}`);
85
- } catch (e) {
86
- logger.warn("Failed to parse params:", e);
87
- }
88
- }
89
- }
90
-
91
64
  private async loadComponent(componentName: string) {
92
65
  // 防止重复加载
93
66
  if (!this.connected || this.isLoading || this.componentInstance) {
94
- logger.debug(
95
- `Skipping loadComponent for ${componentName}: connected=${this.connected}, isLoading=${this.isLoading}, hasInstance=${!!this.componentInstance}`
96
- );
97
67
  return;
98
68
  }
99
-
100
- logger.debug(`WsxView: Loading component ${componentName}`);
101
69
  this.isLoading = true; // 设置标志
102
70
 
103
71
  try {