【Vue3 从入门到实战 进阶式掌握完整知识体系】032-Composition API:toRef和context

3、toRef和context

使用toRef

解构的属性可能不存在,使用toRef给一个默认值,就能够在以后改变的时候实现响应式;

但并不建议这么做,无数据的时候可以给空,或者默认值;

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>hello vue</title>
  <!-- 引入Vue库 -->
  <script src="https://unpkg.com/vue@next"></script>
</head>

<body>
  <div id="root"></div>
</body>

<script>

  const app = Vue.createApp({
    // 我们想象中 2s 后 这里使用的 name 的值也会变成“大哥刘备”
    template: `
      <div>age: {{age}}</div>
    `,
    setup(props, context){
      // 从 vue 引入 reactive
      const { reactive, toRef } = Vue;
      // 定义一个变量 nameObj 对象
      let nameObj = reactive({name: 'zibo'});
      const age = toRef(nameObj, 'age');
      // 2s 后改变其内容
      setTimeout(() => {
        age.value = 25;
      }, 2000);
      return{
        age
      }
    }
  });

  const vm = app.mount('#root');
</script>

</html>

运行结果

image-20210614171532315

context中的attrs和slots

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>hello vue</title>
  <!-- 引入Vue库 -->
  <script src="https://unpkg.com/vue@next"></script>
</head>

<body>
  <div id="root"></div>
</body>

<script>

  // 我们在父组件模板里面使用子组件 test 的时候,传参数
  const app = Vue.createApp({
    template: `
      <test app="app">parent</test>
    `,
  });

  app.component('test', {
    // template:`
    //   <div>child</div>
    // `,
    setup(props, context){
      const { h } = Vue;
      const { attrs, slots, emit } = context;
      console.log("attrs.app:", attrs.app); // 父组件传递过来的 None-Props 属性
      console.log("slots.default():", slots.default()[0].children); // parent
      console.log("slots.default():", slots.default()); // 获取插槽内容
      // 不写 template ,使得 setup 函数返回一个虚拟 DOM 函数
      return () => h('div', {}, slots.default());
    }
  });

  const vm = app.mount('#root');
</script>

</html>

运行结果

image-20210615211426574

context中的emit

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>hello vue</title>
  <!-- 引入Vue库 -->
  <script src="https://unpkg.com/vue@next"></script>
</head>

<body>
  <div id="root"></div>
</body>

<script>

  // 我们在父组件模板里面使用子组件 test 的时候,传参数
  const app = Vue.createApp({
    methods: {
      handleClick(){
        console.log("父组件里面的 handleClick 事件被触发了!");
      }
    },
    template: `
      <test @handleClick="handleClick">parent</test>
    `,
  });

  app.component('test', {
    template:`
      <div @click="handleClick">点我对外触发事件</div>
    `,
    setup(props, context){
      const { h } = Vue;
      const { emit } = context;
      // emit 用于子组件对外触发事件
      function handleClick(){
        emit('handleClick');
      }
      return {
        handleClick
      }
    }
  });

  const vm = app.mount('#root');
</script>

</html>

运行结果

image-20210615211924804

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
A year and a half year ago, I published this article to the Codeguru site and got a number of requests about the Kriging algorithm contour map. Unfortunately, my project was changed shortly after that article and later I quit the company so I couldn‘t find time to finish this Contour business. A week ago, I happened to need a contour map again so I decided to solve the Kriging algorithm. I searched the Internet for a commercial library but they all look ugly and hard to use. So, I made up my mind to make my own algorithm. The Kriging algorithm is easy to find, but this algorithm needs a Matrix and solver (LU-Decomposition). Again, I couldn‘t find suitable code for this. I tried to use GSL first but this made my code too big and was slower. Finally, I went back to "Numerical Recipe in C"—yes, that horrible-looking C code—and changed the code there to my taste.If you read this article before, the rendering part hasn‘t been changed much. I added the Kriging algorithm and revised the codes a little bit. Following is the Kriging Algorithm:templatedouble GetDistance(const ForwardIterator start, int i, int j){ return ::sqrt(::pow(((*(start+i)).x - (*(start+j)).x), 2) + ::pow(((*(start+i)).y - (*(start+j)).y), 2));}templatedouble GetDistance(double xpos, double ypos, const ForwardIterator start, int i){ return ::sqrt(::pow(((*(start+i)).x - xpos), 2) + ::pow(((*(start+i)).y - ypos), 2));}templateclass TKriging : public TInterpolater{public: TKriging(const ForwardIterator first, const ForwardIterator last, double dSemivariance) : m_dSemivariance(dSemivariance) { m_nSize = 0; ForwardIterator start = first; while(start != last) { ++m_nSize; ++start; } m_matA.SetDimension(m_nSize, m_nSize); for(int j=0; j<m_nSize; j++) { for(int i=0; i<m_nSize; i++) { if(i == m_nSize-1 || j == m_nSize-1) { m_matA(i, j) = 1; if(i == m_nSize-1 && j == m_nSize-1) m_matA(i, j) = 0; continue; } m
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值