当前位置:首页 > JavaScript

js 实现component

2026-02-02 10:39:04JavaScript

实现组件的基本方法

在JavaScript中实现组件通常涉及创建可复用的代码块,这些代码块可以封装特定的功能或UI元素。以下是几种常见的实现方式:

使用类(Class)实现组件

class ButtonComponent {
  constructor(text) {
    this.text = text;
  }

  render() {
    const button = document.createElement('button');
    button.textContent = this.text;
    return button;
  }
}

const myButton = new ButtonComponent('Click me');
document.body.appendChild(myButton.render());

使用工厂函数实现组件

function createButton(text) {
  const button = document.createElement('button');
  button.textContent = text;

  return {
    element: button,
    setText(newText) {
      button.textContent = newText;
    }
  };
}

const button = createButton('Submit');
document.body.appendChild(button.element);

使用现代框架实现组件

React函数组件示例

import React from 'react';

function Greeting({ name }) {
  return <h1>Hello, {name}!</h1>;
}

export default Greeting;

Vue单文件组件示例

<template>
  <div>
    <h1>{{ greeting }}</h1>
    <button @click="changeGreeting">Change</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      greeting: 'Hello World'
    };
  },
  methods: {
    changeGreeting() {
      this.greeting = 'Hello Vue!';
    }
  }
};
</script>

组件通信模式

父子组件通信(React示例)

// Parent.js
function Parent() {
  const [count, setCount] = useState(0);

  return (
    <div>
      <Child count={count} onIncrement={() => setCount(c => c + 1)} />
    </div>
  );
}

// Child.js
function Child({ count, onIncrement }) {
  return (
    <div>
      <p>Count: {count}</p>
      <button onClick={onIncrement}>Increment</button>
    </div>
  );
}

事件总线模式(Vue示例)

// eventBus.js
import Vue from 'vue';
export const EventBus = new Vue();

// ComponentA.vue
EventBus.$emit('message', 'Hello from A');

// ComponentB.vue
EventBus.$on('message', (msg) => {
  console.log(msg); // 'Hello from A'
});

组件生命周期管理

类组件生命周期(React)

class LifecycleDemo extends React.Component {
  componentDidMount() {
    console.log('Component mounted');
  }

  componentDidUpdate() {
    console.log('Component updated');
  }

  componentWillUnmount() {
    console.log('Component will unmount');
  }

  render() {
    return <div>Lifecycle Demo</div>;
  }
}

组合式API生命周期(Vue 3)

import { onMounted, onUpdated, onUnmounted } from 'vue';

export default {
  setup() {
    onMounted(() => {
      console.log('Component mounted');
    });

    onUpdated(() => {
      console.log('Component updated');
    });

    onUnmounted(() => {
      console.log('Component will unmount');
    });
  }
};

组件样式封装

CSS Modules(React)

import styles from './Button.module.css';

function Button() {
  return <button className={styles.primary}>Click</button>;
}

Scoped Styles(Vue)

js 实现component

<template>
  <button class="primary">Click</button>
</template>

<style scoped>
.primary {
  background: blue;
}
</style>

这些方法展示了从原生JavaScript到现代框架的组件实现方式,开发者可以根据项目需求选择合适的方法。组件化开发的核心思想是关注点分离和可复用性,良好的组件设计应该遵循单一职责原则。

标签: jscomponent
分享给朋友:

相关文章

js实现跳转

js实现跳转

使用 window.location 跳转 通过修改 window.location.href 实现页面跳转,这是最常用的方法。 window.location.href = 'https://…

js实现

js实现

实现 JavaScript 功能的方法 在 JavaScript 中实现特定功能通常涉及多个步骤。以下是常见的实现方法: 基础语法和变量声明 使用 let 或 const 声明变量: let co…

js实现继承

js实现继承

原型链继承 通过将子类的原型指向父类的实例实现继承。子类实例可以访问父类原型上的属性和方法。 function Parent() { this.name = 'parent'; } Parent…

js分组实现

js分组实现

分组实现方法 在JavaScript中,可以通过多种方式实现数组或对象的分组操作。以下是几种常见的方法: 使用Array.prototype.reduce() 通过reduce方法可以轻松实现数组分…

js 实现验证码

js 实现验证码

实现验证码的 JavaScript 方法 生成随机验证码 验证码通常由随机字符(数字、字母或混合)组成。以下代码生成一个 6 位随机验证码(数字和字母混合): function generateCa…

js 实现mvc

js 实现mvc

MVC 模式简介 MVC(Model-View-Controller)是一种软件设计模式,将应用程序分为三个核心组件:模型(Model)处理数据和业务逻辑,视图(View)负责展示数据,控制器(Con…