资讯专栏INFORMATION COLUMN

【全栈React】第8天: 属性类型

codeKK / 3344人阅读

摘要:在大多数情况下,我们期望这些是一种特定类型或一组类型也称为或。例如,几天前我们构建的组件接受一个称为的属性,我们期望它是一个字符串。必需类型可以通过在任意个属性类型中附加中描述来将需要传递给一个组件根据需要设置是非常有用的。

本文转载自:众成翻译
译者:iOSDevLog
链接:http://www.zcfy.cc/article/3818
原文:https://www.fullstackreact.com/30-days-of-react/day-8/

我们正在考虑如何在今天重新使用React组件,这样我们不仅可以在应用而且可以在团队中共享我们的组件。

唷! 我们做了第二周(相对毫发)! 通过这一点,我们已经讨论了React(propsstate,生命周期挂钩函数,JSX等)的大部分基本特性。 在本节中,我们将看一下注解我们的组件。

PropTypes

您可能已经注意到我们在组件中使用了props 。 在大多数情况下,我们期望这些是一种特定类型或一组类型(也称为objectstring)。 React提供了一种定义和验证这些类型的方法,使我们能够轻松暴露组件API。

文档化不仅是好习惯,对于构建也是有益的reusable react components.

React.PropTypes对象导出一堆不同的类型,我们可以用它来定义组件的prop应该是什么类型的。 我们可以在ES6类风格的React prop中使用propTypes方法来定义它们:

class Clock extends React.Component {
  // ...
}

Clock.propTypes = {
  // key is the name of the prop and
  // value is the PropType
}

使用React.createClass() 形式, 我们定义一个propTypeskey
例如,我们可以将Clock组件的形式重写为:

const Clock = React.createClass({
  proptypes: {}
}); 

从这个prop里面,我们可以定义一个对象,它具有一个prop的key作为我们定义的prop的名称,它应被定义的类型(或类型)的一个值。

例如,几天前我们构建的Header 组件接受一个称为title 的属性,我们期望它是一个字符串。 我们可以将其类型定义为字符串:

class Header extends React.Component {
  // ...
}

Header.propTypes = {
  title: React.PropTypes.string
}

React有很多类型可供选择,在React.PropTypes 对象上导出,甚至允许我们定义一个自定义的对象类型。 看看可用类型的总体列表:

[](#basic-types)基本类型

React暴露了我们可以开箱即用的几种基本类型。

类型 例子
String "hello" React.PropTypes.string
Number 10, 0.1 React.PropTypes.number
Boolean true / false React.PropTypes.bool
Function const say => (msg) => console.log("Hello world") React.PropTypes.func
Symbol Symbol("msg") React.PropTypes.symbol
Object {name: "Ari"} React.PropTypes.object
Anything "whatever", 10, {}

可以告诉React我们希望它传递_anything_可以使用React.PropTypes.node来呈现:

类型 例子
A rendererable 10, "hello" React.PropTypes.node
Clock.propTypes = {
  title: React.PropTypes.string,
  count: React.PropTypes.number,
  isOn: React.PropTypes.bool,
  onDisplay: React.PropTypes.func,
  symbol: React.PropTypes.symbol,
  user: React.PropTypes.object,

  name: React.PropTypes.node
}

我们已经看过如何使用props从父组件到子组件进行通信。 我们可以使用函数从子组件到父组件进行通信。 当我们想要操作一个子组件的父组件时,我们会经常使用这种模式。

集合类型

我们可以通过我们的props中的可迭代的集合。 当我们通过我们的活动通过一个数组时,我们已经看到了如何做到这一点。 要将组件的proptype声明为数组,我们可以使用 React.PropTypes.array 注解。

我们也可以要求数组只能使用某种类型的对象 React.PropTypes.arrayOf([]).

类型 例子
Array [] React.PropTypes.array
Array of numbers [1, 2, 3] React.PropTypes.arrayOf([type])
Enum ["Red", "Blue"] React.PropTypes.oneOf([arr])

可以使用React.PropTypes.oneOfType([types])来描述可以是几种不同类型之一的对象。

Clock.propTypes = {
  counts: React.PropTypes.array,
  users: React.PropTypes.arrayOf(React.PropTypes.object),
  alarmColor: React.PropTypes.oneOf(["red", "blue"]),
  description: React.PropTypes.oneOfType([
      React.PropTypes.string,
      React.PropTypes.instanceOf(Title)
    ]),
}
对象类型

可以定义需要某个特定类型的特定类型或实例的类型。

类型 例子
Object {name: "Ari"} React.PropTypes.object
Number object {count: 42} React.PropTypes.objectOf()
Instance new Message() React.PropTypes.objectOf()
Object shape {name: "Ari"} React.PropTypes.shape()
Clock.propTypes = {
  basicObject: React.PropTypes.object,

  numbers: React.PropTypes
    .objectOf(React.PropTypes.numbers),

  messages: React.PropTypes
    .instanceOf(Message),

  contactList: React.PropTypes.shape({
    name: React.PropTypes.string,
    phone: React.PropTypes.string,
  })
}
React类型

我们也可以通过React元素从父组件到子组件。 这对于构建模板和提供模板的定制非常有用。

类型 例子
Element </b></td> <td align="right"><b>React.PropTypes.element</b></td> </tr></tbody> </table> <pre>Clock.propTypes = { displayEle: React.PropTypes.element } </pre> <p>当我们使用_element_时,React希望我们能够接受一个多带带的子组件。 也就是说,我们将无法传递多个元素。</p> <pre>// Invalid for elements <Clock displayElement={ <div>Name</div> <div>Age</div> }></Clock> // Valid <Clock displayElement={ <div> <div>Name</div> <div>Age</div> </div> }></Clock> </pre> <b>必需类型</b> <p>可以通过在_任意_个属性类型中附加<b>.isRequired</b>中描述来将需要传递给一个组件:</p> <pre>Clock.propTypes = { title: React.PropTypes.name.isRequired, } </pre> <p>根据需要设置<b>prop</b>是非常有用的。当组件依赖于一个<b>prop</b>被它的父组件传递,如果没有它将不会工作。</p> <b>自定义类型</b> <p>最后,还可以传递一个函数来定义自定义类型。 我们可以做一个单一属性或验证数组。 自定义函数的一个要求是,如果验证确定_not_ 传递,则期望我们返回一个 <b>Error</b> 对象:</p> <table> <thead><tr> <th>类型</th> <th align="center">例子</th> <th align="right">类</th> </tr></thead> <tbody> <tr> <td>Custom</td> <td align="center">"something_crazy"</td> <td align="right"><b>function(props, propName, componentName) {}</b></td> </tr> <tr> <td>CustomArray</td> <td align="center">["something", "crazy"]</td> <td align="right"><b>React.PropTypes.arrayOf(function(props, propName, componentName) {})</b></td> </tr> </tbody> </table> <pre>UserLink.propTypes = { userWithName: (props, propName, componentName) => { if (!props[propName] || !props[propName].name) { return new Error( "Invalid " + propName + ": No name property defined for component " + componentName ) } } } </pre> <b>默认属性</b> <p>rendered, so we can define a common title instead by setting it"s default prop value.有时,我们希望能够为属性设置默认值。 例如,我们昨天构建的<b>组件</b>可能不需要传递标题。 如果不是,我们仍然需要一个标题来渲染,所以我们可以通过设置它的默认支持值来定义一个通用的标题。</p> <p>要设置默认的prop值,我们可以在组件上使用<b>defaultProps</b>对象键。</p> <pre>Header.defaultProps = { title: "Github activity" } </pre> <pre> <p>当使用<b>React.createClass()</b> 形式时,我们可以定义一个名为<b>getDefaultProps()</b> 的对象键,该对象键将返回具有默认值道具的对象。</p> <pre>React.createClass({ getDefaultProps: () => ({ name: "Github activity" }) }) </pre> </pre> <p>呃,今天我们浏览了很多文档。 使用组件的<b>propTypes</b> 和<b>defaultProps</b> 属性构建可重用组件是一个好主意。 不仅可以使开发人员之间进行沟通变得更加容易,而且在离开组件几天后我们也可以轻松回收。 接下来,我们将回到代码,并开始将某些风格整合到我们的组件中。</p> </div> <div class="mt-64 tags-seach" > <div class="tags-info"> <a style="width:120px;" title="私有云" href="https://www.ucloud.cn/site/product/ucloudstack.html">私有云</a> <a style="width:120px;" title="idc机房托管" href="https://www.ucloud.cn/site/product/uxzone.html">idc机房托管</a> <a style="width:120px;" title="javascript基础教程第8版" href="https://www.ucloud.cn/yun/tag/javascriptjichujiaochengdi8ban/">javascript基础教程第8版</a> <a style="width:120px;" title="js 获取第一个属性" href="https://www.ucloud.cn/yun/tag/js huoqudiyigeshuxing/">js 获取第一个属性</a> <a style="width:120px;" title="linux 8位数据类型" href="https://www.ucloud.cn/yun/tag/linux 8weishujuleixing/">linux 8位数据类型</a> <a style="width:120px;" title="工资项目属性数据类型" href="https://www.ucloud.cn/yun/tag/gongzixiangmushuxingshujuleixing/">工资项目属性数据类型</a> </div> </div> <div class="entry-copyright mb-30"> <p class="mb-15"> 文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。</p> <p>转载请注明本文地址:https://www.ucloud.cn/yun/84671.html</p> </div> <ul class="pre-next-page"> <li class="ellipsis"><a class="hpf" href="https://www.ucloud.cn/yun/84670.html">上一篇:【全栈React】第9天: 样式</a></li> <li class="ellipsis"><a class="hpf" href="https://www.ucloud.cn/yun/84672.html">下一篇:【全栈React】第7天: 生命周期钩子函数</a></li> </ul> </div> <div class="about_topicone-mid"> <h3 class="top-com-title mb-0"><span data-id="0">相关文章</span></h3> <ul class="com_white-left-mid atricle-list-box"> <li> <div class="atricle-list-right"> <h2 class="ellipsis2"><a class="hpf" href="https://www.ucloud.cn/yun/84665.html"><b>【<em>全栈</em><em>React</em>】<em>React</em> 30<em>天</em>教程索引</b></a></h2> <p class="ellipsis2 good">摘要:今天我们将讨论创建组件的最终方案,即无状态函数的纯组件。今天我们正在研究一种处理提出的复杂数据的方法,称为体系结构。第天部署介绍今天,我们将探讨部署我们的应用所涉及的不同部分,以便外界可以使用我们的应用。 本文转载自:众成翻译译者:iOSDevLog链接:http://www.zcfy.cc/article/3758原文:https://www.fullstackreact.com/3...</p> <div class="com_white-left-info"> <div class="com_white-left-infol"> <a href="https://www.ucloud.cn/yun/u-1451.html"><img src="https://www.ucloud.cn/yun/data/avatar/000/00/14/small_000001451.jpg" alt=""><span class="layui-hide64">appetizerio</span></a> <time datetime="">2019-08-20 18:33</time> <span><i class="fa fa-commenting"></i>评论0</span> <span><i class="fa fa-star"></i>收藏0</span> </div> </div> </div> </li> <li> <div class="atricle-list-right"> <h2 class="ellipsis2"><a class="hpf" href="https://www.ucloud.cn/yun/84725.html"><b>【<em>全栈</em><em>React</em>】<em>第</em>17<em>天</em>: 客户端路由</b></a></h2> <p class="ellipsis2 good">摘要:但是使用标记将告诉浏览器处理路由就像服务器端路由一样。组件需要一个称为的属性指向要渲染的客户端路由。发生这种情况的原因是响应路由器将渲染与路径匹配的所有内容除非另有指定。属性预计将是一个函数将在对象连同和路由配置时调用。 本文转载自:众成翻译译者:iOSDevLog链接:http://www.zcfy.cc/article/3815原文:https://www.fullstackrea...</p> <div class="com_white-left-info"> <div class="com_white-left-infol"> <a href="https://www.ucloud.cn/yun/u-227.html"><img src="https://www.ucloud.cn/yun/data/avatar/000/00/02/small_000000227.jpg" alt=""><span class="layui-hide64">harriszh</span></a> <time datetime="">2019-08-20 18:35</time> <span><i class="fa fa-commenting"></i>评论0</span> <span><i class="fa fa-star"></i>收藏0</span> </div> </div> </div> </li> <li> <div class="atricle-list-right"> <h2 class="ellipsis2"><a class="hpf" href="https://www.ucloud.cn/yun/87164.html"><b>【<em>全栈</em><em>React</em>】<em>第</em>22<em>天</em>: 测试简介</b></a></h2> <p class="ellipsis2 good">摘要:我们将讨论三种不同的软件测试范例单元测试功能测试和集成测试。在中单元测试通常不需要浏览器可以快速运行不需要写入断言本身通常是简单而简洁的。集成测试最后我们将研究的最后一种测试是集成测试。 本文转载自:众成翻译译者:iOSDevLog链接:http://www.zcfy.cc/article/3809原文:https://www.fullstackreact.com/30-days-of...</p> <div class="com_white-left-info"> <div class="com_white-left-infol"> <a href="https://www.ucloud.cn/yun/u-1160.html"><img src="https://www.ucloud.cn/yun/data/avatar/000/00/11/small_000001160.jpg" alt=""><span class="layui-hide64">qc1iu</span></a> <time datetime="">2019-08-21 11:50</time> <span><i class="fa fa-commenting"></i>评论0</span> <span><i class="fa fa-star"></i>收藏0</span> </div> </div> </div> </li> <li> <div class="atricle-list-right"> <h2 class="ellipsis2"><a class="hpf" href="https://www.ucloud.cn/yun/84722.html"><b>【<em>全栈</em><em>React</em>】<em>第</em>20<em>天</em>: Redux动作</b></a></h2> <p class="ellipsis2 good">摘要:去营救有一种方法我们把我们的归约器分成多个归约器每个都只负责状态树的叶子。此外我们还学习了如何扩展以使用多个归约器和动作以及多个连接的组件。 本文转载自:众成翻译译者:iOSDevLog链接:http://www.zcfy.cc/article/3825原文:https://www.fullstackreact.com/30-days-of-react/day-20/ 使用Redux,...</p> <div class="com_white-left-info"> <div class="com_white-left-infol"> <a href="https://www.ucloud.cn/yun/u-655.html"><img src="https://www.ucloud.cn/yun/data/avatar/000/00/06/small_000000655.jpg" alt=""><span class="layui-hide64">SimonMa</span></a> <time datetime="">2019-08-20 18:35</time> <span><i class="fa fa-commenting"></i>评论0</span> <span><i class="fa fa-star"></i>收藏0</span> </div> </div> </div> </li> <li> <div class="atricle-list-right"> <h2 class="ellipsis2"><a class="hpf" href="https://www.ucloud.cn/yun/84685.html"><b>【<em>全栈</em><em>React</em>】<em>第</em>13<em>天</em>: 重复元素</b></a></h2> <p class="ellipsis2 good">摘要:在我们的应用中添加太多的复杂度来加载外部数据之前今天我们将快速了解如何在应用中重复组件元素。出于性能原因使用虚拟尝试限制在重新视图时需要更新的元素的数量。 本文转载自:众成翻译译者:iOSDevLog链接:http://www.zcfy.cc/article/3826原文:https://www.fullstackreact.com/30-days-of-react/day-13/ 今...</p> <div class="com_white-left-info"> <div class="com_white-left-infol"> <a href="https://www.ucloud.cn/yun/u-1554.html"><img src="https://www.ucloud.cn/yun/data/avatar/000/00/15/small_000001554.jpg" alt=""><span class="layui-hide64">RaoMeng</span></a> <time datetime="">2019-08-20 18:33</time> <span><i class="fa fa-commenting"></i>评论0</span> <span><i class="fa fa-star"></i>收藏0</span> </div> </div> </div> </li> </ul> </div> <div class="topicone-box-wangeditor"> <h3 class="top-com-title mb-64"><span>发表评论</span></h3> <div class="xcp-publish-main flex_box_zd"> <div class="unlogin-pinglun-box"> <a href="javascript:login()" class="grad">登陆后可评论</a> </div> </div> </div> <div class="site-box-content"> <div class="site-content-title"> <h3 class="top-com-title mb-64"><span>0条评论</span></h3> </div> <div class="pages"></ul></div> </div> </div> <div class="layui-col-md4 layui-col-lg3 com_white-right site-wrap-right"> <div class=""> <div class="com_layuiright-box user-msgbox"> <a href="https://www.ucloud.cn/yun/u-1299.html"><img src="https://www.ucloud.cn/yun/data/avatar/000/00/12/small_000001299.jpg" alt=""></a> <h3><a href="https://www.ucloud.cn/yun/u-1299.html" rel="nofollow">codeKK</a></h3> <h6>男<span>|</span>高级讲师</h6> <div class="flex_box_zd user-msgbox-atten"> <a href="javascript:attentto_user(1299)" id="attenttouser_1299" class="grad follow-btn notfollow attention">我要关注</a> <a href="javascript:login()" title="发私信" >我要私信</a> </div> <div class="user-msgbox-list flex_box_zd"> <h3 class="hpf">TA的文章</h3> <a href="https://www.ucloud.cn/yun/ut-1299.html" class="box_hxjz">阅读更多</a> </div> <ul class="user-msgbox-ul"> <li><h3 class="ellipsis"><a href="https://www.ucloud.cn/yun/121704.html">debian系统更换软件源镜像优化国内下载速度</a></h3> <p>阅读 2699<span>·</span>2021-09-30 09:59</p></li> <li><h3 class="ellipsis"><a href="https://www.ucloud.cn/yun/119898.html">记录使用微PE工具箱快速制作U盘系统盘安装系统工具</a></h3> <p>阅读 1882<span>·</span>2021-09-13 10:34</p></li> <li><h3 class="ellipsis"><a href="https://www.ucloud.cn/yun/115204.html">打造专属自己的html5拼图小游戏</a></h3> <p>阅读 413<span>·</span>2019-08-30 12:58</p></li> <li><h3 class="ellipsis"><a href="https://www.ucloud.cn/yun/114519.html">TypeScript 、React、 Redux和Ant-Design的最佳实践</a></h3> <p>阅读 1337<span>·</span>2019-08-29 18:42</p></li> <li><h3 class="ellipsis"><a href="https://www.ucloud.cn/yun/109406.html">阿里云携手优锘发布智慧园区可视化产品</a></h3> <p>阅读 2021<span>·</span>2019-08-26 13:44</p></li> <li><h3 class="ellipsis"><a href="https://www.ucloud.cn/yun/104839.html">【Vue项目总结】后台管理项目总结</a></h3> <p>阅读 2808<span>·</span>2019-08-23 18:12</p></li> <li><h3 class="ellipsis"><a href="https://www.ucloud.cn/yun/101631.html">JavaScript数据结构与算法——数组</a></h3> <p>阅读 3236<span>·</span>2019-08-23 15:10</p></li> <li><h3 class="ellipsis"><a href="https://www.ucloud.cn/yun/100880.html">马上收藏!史上最全正则表达式合集</a></h3> <p>阅读 1513<span>·</span>2019-08-23 14:37</p></li> </ul> </div> <!-- 文章详情右侧广告--> <div class="com_layuiright-box"> <h6 class="top-com-title"><span>最新活动</span></h6> <div class="com_adbox"> <div class="layui-carousel" id="right-item"> <div carousel-item> <div> <a href="https://www.ucloud.cn/site/product/uhost.html" rel="nofollow"> <img src="https://www.ucloud.cn/yun/data/attach/220620/Z7TLrpAi.png" alt="云服务器"> </a> </div> <div> <a href="https://www.ucloud.cn/site/product/uhybrid.html" rel="nofollow"> <img src="https://www.ucloud.cn/yun/data/attach/220620/MWraMsBh.png" alt="混合云"> </a> </div> <div> <a href="https://www.ucloud.cn/site/product/ucloudstack.html" rel="nofollow"> <img src="https://www.ucloud.cn/yun/data/attach/220620/ifzOxvjW.png" alt="私有云"> </a> </div> <div> <a href="https://www.ucloud.cn/site/product/utrion.html" rel="nofollow"> <img src="https://www.ucloud.cn/yun/data/attach/220620/VDqwC1iN.png" alt="超融合服务器"> </a> </div> <div> <a href="https://www.ucloud.cn/site/product/uhybrid.html" rel="nofollow"> <img src="https://www.ucloud.cn/yun/data/attach/220630/pJwnviKN.png" alt="服务器托管"> </a> </div> <div> <a href="https://www.ucloud.cn/site/product/uxzone.html" rel="nofollow"> <img src="https://www.ucloud.cn/yun/data/attach/220630/CDb5uXxp.jpeg" alt="idc机房托管"> </a> </div> <div> <a href="https://www.ucloud.cn/site/active/network.html?ytag=seo" rel="nofollow"> <img src="https://www.ucloud.cn/yun/data/attach/230227/XWsSXmvm.png" alt="专线服务"> </a> </div> </div> </div> </div> <!-- banner结束 --> <div class="adhtml"> </div> <script> $(function(){ $.ajax({ type: "GET", url:"https://www.ucloud.cn/yun/ad/getad/1.html", cache: false, success: function(text){ $(".adhtml").html(text); } }); }) </script> </div> </div> </div> </div> </div> </section> <!-- wap拉出按钮 --> <div class="site-tree-mobile layui-hide"> <i class="layui-icon layui-icon-spread-left"></i> </div> <!-- wap遮罩层 --> <div class="site-mobile-shade"></div> <!--付费阅读 --> <div id="payread"> <div class="layui-form-item">阅读需要支付1元查看</div> <div class="layui-form-item"><button class="btn-right">支付并查看</button></div> </div> <script> var prei=0; $(".site-seo-depict pre").each(function(){ var html=$(this).html().replace("<code>","").replace("</code>","").replace('<code class="javascript hljs" codemark="1">',''); $(this).attr('data-clipboard-text',html).attr("id","pre"+prei); $(this).html("").append("<code>"+html+"</code>"); prei++; }) $(".site-seo-depict img").each(function(){ if($(this).attr("src").indexOf('data:image/svg+xml')!= -1){ $(this).remove(); } }) $("LINK[href*='style-49037e4d27.css']").remove(); $("LINK[href*='markdown_views-d7a94ec6ab.css']").remove(); layui.use(['jquery', 'layer','code'], function(){ $("pre").attr("class","layui-code"); $("pre").attr("lay-title",""); $("pre").attr("lay-skin",""); layui.code(); $(".layui-code-h3 a").attr("class","copycode").html("复制代码 ").attr("onclick","copycode(this)"); }); function copycode(target){ var id=$(target).parent().parent().attr("id"); var clipboard = new ClipboardJS("#"+id); clipboard.on('success', function(e) { e.clearSelection(); alert("复制成功") }); clipboard.on('error', function(e) { alert("复制失败") }); } //$(".site-seo-depict").html($(".site-seo-depict").html().slice(0, -5)); </script> <link rel="stylesheet" type="text/css" href="https://www.ucloud.cn/yun/static/js/neweditor/code/styles/tomorrow-night-eighties.css"> <script src="https://www.ucloud.cn/yun/static/js/neweditor/code/highlight.pack.js" type="text/javascript"></script> <script src="https://www.ucloud.cn/yun/static/js/clipboard.js"></script> <script>hljs.initHighlightingOnLoad();</script> <script> function setcode(){ var _html=''; document.querySelectorAll('pre code').forEach((block) => { var _tmptext=$.trim($(block).text()); if(_tmptext!=''){ _html=_html+_tmptext; console.log(_html); } }); } </script> <script> function payread(){ layer.open({ type: 1, title:"付费阅读", shadeClose: true, content: $('#payread') }); } // 举报 function jupao_tip(){ layer.open({ type: 1, title:false, shadeClose: true, content: $('#jubao') }); } $(".getcommentlist").click(function(){ var _id=$(this).attr("dataid"); var _tid=$(this).attr("datatid"); $("#articlecommentlist"+_id).toggleClass("hide"); var flag=$("#articlecommentlist"+_id).attr("dataflag"); if(flag==1){ flag=0; }else{ flag=1; //加载评论 loadarticlecommentlist(_id,_tid); } $("#articlecommentlist"+_id).attr("dataflag",flag); }) $(".add-comment-btn").click(function(){ var _id=$(this).attr("dataid"); $(".formcomment"+_id).toggleClass("hide"); }) $(".btn-sendartcomment").click(function(){ var _aid=$(this).attr("dataid"); var _tid=$(this).attr("datatid"); var _content=$.trim($(".commenttext"+_aid).val()); if(_content==''){ alert("评论内容不能为空"); return false; } var touid=$("#btnsendcomment"+_aid).attr("touid"); if(touid==null){ touid=0; } addarticlecomment(_tid,_aid,_content,touid); }) $(".button_agree").click(function(){ var supportobj = $(this); var tid = $(this).attr("id"); $.ajax({ type: "GET", url:"https://www.ucloud.cn/yun/index.php?topic/ajaxhassupport/" + tid, cache: false, success: function(hassupport){ if (hassupport != '1'){ $.ajax({ type: "GET", cache:false, url: "https://www.ucloud.cn/yun/index.php?topic/ajaxaddsupport/" + tid, success: function(comments) { supportobj.find("span").html(comments+"人赞"); } }); }else{ alert("您已经赞过"); } } }); }); function attenquestion(_tid,_rs){ $.ajax({ //提交数据的类型 POST GET type:"POST", //提交的网址 url:"https://www.ucloud.cn/yun/favorite/topicadd.html", //提交的数据 data:{tid:_tid,rs:_rs}, //返回数据的格式 datatype: "json",//"xml", "html", "script", "json", "jsonp", "text". //在请求之前调用的函数 beforeSend:function(){}, //成功返回之后调用的函数 success:function(data){ var data=eval("("+data+")"); console.log(data) if(data.code==2000){ layer.msg(data.msg,function(){ if(data.rs==1){ //取消收藏 $(".layui-layer-tips").attr("data-tips","收藏文章"); $(".layui-layer-tips").html('<i class="fa fa-heart-o"></i>'); } if(data.rs==0){ //收藏成功 $(".layui-layer-tips").attr("data-tips","已收藏文章"); $(".layui-layer-tips").html('<i class="fa fa-heart"></i>') } }) }else{ layer.msg(data.msg) } } , //调用执行后调用的函数 complete: function(XMLHttpRequest, textStatus){ postadopt=true; }, //调用出错执行的函数 error: function(){ //请求出错处理 postadopt=false; } }); } </script> <footer> <div class="layui-container"> <div class="flex_box_zd"> <div class="left-footer"> <h6><a href="https://www.ucloud.cn/"><img src="https://www.ucloud.cn/yun/static/theme/ukd//images/logo.png" alt="UCloud (优刻得科技股份有限公司)"></a></h6> <p>UCloud (优刻得科技股份有限公司)是中立、安全的云计算服务平台,坚持中立,不涉足客户业务领域。公司自主研发IaaS、PaaS、大数据流通平台、AI服务平台等一系列云计算产品,并深入了解互联网、传统企业在不同场景下的业务需求,提供公有云、混合云、私有云、专有云在内的综合性行业解决方案。</p> </div> <div class="right-footer layui-hidemd"> <ul class="flex_box_zd"> <li> <h6>UCloud与云服务</h6> <p><a href="https://www.ucloud.cn/site/about/intro/">公司介绍</a></p> <p><a href="https://zhaopin.ucloud.cn/" >加入我们</a></p> <p><a href="https://www.ucloud.cn/site/ucan/onlineclass/">UCan线上公开课</a></p> <p><a href="https://www.ucloud.cn/site/solutions.html" >行业解决方案</a></p> <p><a href="https://www.ucloud.cn/site/pro-notice/">产品动态</a></p> </li> <li> <h6>友情链接</h6> <p><a href="https://www.compshare.cn/?ytag=seo">GPU算力平台</a></p> <p><a href="https://www.ucloudstack.com/?ytag=seo">UCloud私有云</a></p> <p><a href="https://www.surfercloud.com/">SurferCloud</a></p> <p><a href="https://www.uwin-link.com/">工厂仿真软件</a></p> <p><a href="https://pinex.it/">Pinex</a></p> <p><a href="https://www.picpik.ai/zh">AI绘画</a></p> </li> <li> <h6>社区栏目</h6> <p><a href="https://www.ucloud.cn/yun/column/index.html">专栏文章</a></p> <p><a href="https://www.ucloud.cn/yun/udata/">专题地图</a></p> </li> <li> <h6>常见问题</h6> <p><a href="https://www.ucloud.cn/site/ucsafe/notice.html" >安全中心</a></p> <p><a href="https://www.ucloud.cn/site/about/news/recent/" >新闻动态</a></p> <p><a href="https://www.ucloud.cn/site/about/news/report/">媒体动态</a></p> <p><a href="https://www.ucloud.cn/site/cases.html">客户案例</a></p> <p><a href="https://www.ucloud.cn/site/notice/">公告</a></p> </li> <li> <span><img src="https://static.ucloud.cn/7a4b6983f4b94bcb97380adc5d073865.png" alt="优刻得"></span> <p>扫扫了解更多</p></div> </div> <div class="copyright">Copyright © 2012-2023 UCloud 优刻得科技股份有限公司<i>|</i><a rel="nofollow" href="http://beian.miit.gov.cn/">沪公网安备 31011002000058号</a><i>|</i><a rel="nofollow" href="http://beian.miit.gov.cn/"></a> 沪ICP备12020087号-3</a><i>|</i> <script type="text/javascript" src="https://gyfk12.kuaishang.cn/bs/ks.j?cI=197688&fI=125915" charset="utf-8"></script> <script> var _hmt = _hmt || []; (function() { var hm = document.createElement("script"); hm.src = "https://hm.baidu.com/hm.js?290c2650b305fc9fff0dbdcafe48b59d"; var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(hm, s); })(); </script> <!-- Global site tag (gtag.js) - Google Analytics --> <script async src="https://www.googletagmanager.com/gtag/js?id=G-DZSMXQ3P9N"></script> <script> window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); gtag('config', 'G-DZSMXQ3P9N'); </script> <script> (function(){ var el = document.createElement("script"); el.src = "https://lf1-cdn-tos.bytegoofy.com/goofy/ttzz/push.js?99f50ea166557aed914eb4a66a7a70a4709cbb98a54ecb576877d99556fb4bfc3d72cd14f8a76432df3935ab77ec54f830517b3cb210f7fd334f50ccb772134a"; el.id = "ttzz"; var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(el, s); })(window) </script></div> </div> </footer> </body> <script src="https://www.ucloud.cn/yun/static/theme/ukd/js/common.js"></script> <<script type="text/javascript"> $(".site-seo-depict *,.site-content-answer-body *,.site-body-depict *").css("max-width","100%"); </script> </html>