Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

希望能支持组件样式内 import 全局样式 #3595

Closed
amoydavid opened this issue Jun 30, 2019 · 9 comments
Closed

希望能支持组件样式内 import 全局样式 #3595

amoydavid opened this issue Jun 30, 2019 · 9 comments

Comments

@amoydavid
Copy link

详见此issus jd-opensource/taro-ui#658

有两个样式,组件的样式 component.scss 全局的样式 'app.scss'

我希望是能在组件样式里直接 @import 全局的样式库。所以有了如下尝试。

我在组件样式component.scss@import '../../app.scss',但直接这样的代码会把整个app.scss编译后放到component.wxss 文件内,并不是我最想要的结果。
如果使用 @import '../../app.css' 那么编译后的代码是 @import '../../app.css',微信小程序这块就是错的。 如果使用 @import '../../app.wxss`,编译会出错。

如果把 component.scss 的文件名改成 component.wxss,并在jsx里import这个wxss,那么在wxss里使用 @import '../../app.wxss' 得到了正确的结果。

是不是能在component.scss里能直接 @import 'path/to/other.wxss' 或是有更优的方案?

@taro-bot
Copy link

taro-bot bot commented Jun 30, 2019

欢迎提交 Issue~

如果你提交的是 bug 报告,请务必遵循 Issue 模板的规范,尽量用简洁的语言描述你的问题,最好能提供一个稳定简单的复现。🙏🙏🙏

如果你的信息提供过于模糊或不足,或者已经其他 issue 已经存在相关内容,你的 issue 有可能会被关闭。

Good luck and happy coding~

@Garfield550
Copy link
Collaborator

全局样式最好不要在页面内或者组件样式内引用。

@amoydavid
Copy link
Author

amoydavid commented Jul 1, 2019 via email

@Garfield550
Copy link
Collaborator

由于 app.js 添加的样式文件 在小程序上只能影响 page 样式,不能影响 component 的样式,所以在使用自定义组件时,你可能需要再次引入

Ref: https://taro-ui.aotu.io/#/docs/flex

除继承样式外, app.wxss 中的样式、组件所在页面的的样式对自定义组件无效(除非更改组件样式隔离选项)。

Ref: https://developers.weixin.qq.com/miniprogram/dev/framework/custom-component/wxml-wxss.html

@amoydavid
Copy link
Author

amoydavid commented Jul 1, 2019 via email

@Garfield550
Copy link
Collaborator

微信限制,没有办法啊。

除继承样式外, app.wxss 中的样式、组件所在页面的的样式对自定义组件无效

折衷方案也只能是在每个用到 Flex 的地方 @import "~taro-ui/dist/style/components/flex.scss"; 一下

@Garfield550
Copy link
Collaborator

另外,可以试试 addGlobalClass,看看这么做能不能在 Component 写 className='at-row' 并应用样式。

此外,小程序基础库版本 2.2.3 以上支持 addGlobalClass 选项,即在 Component 的 options 中设置 addGlobalClass: true 。 这个选项等价于设置 styleIsolation: apply-shared ,但设置了 styleIsolation 选项后这个选项会失效。

Ref: https://developers.weixin.qq.com/miniprogram/dev/framework/custom-component/wxml-wxss.html

使用外部样式类可以让组件使用指定的组件外样式类,如果希望组件外样式类能够完全影响组件内部,可以将组件构造器中的 options.addGlobalClass 字段置为 true。这个特性从小程序基础库版本 2.2.3 开始支持。

Ref: https://nervjs.github.io/taro/docs/component-style.html#%E5%85%A8%E5%B1%80%E6%A0%B7%E5%BC%8F%E7%B1%BB

@amoydavid
Copy link
Author

实际的场景更可能是这样的。
我基于taro-ui实现了一些组件,这些组件多次用到了flex的样式。我比较希望只要写一次flex的样式,其它地方用import进来。这样样式统一,也会减小包的体积,对吧。

用 addGlobalClass 对于taro来说应该不是最佳方案。

@snakeUni
Copy link

我现在在按照 Taro UI 来文件结构在写组件库,但是我在 app.tsx 中设置了 addGlobalClass: true 是无效的,在每个组件中也设置了这个选项
app.tsx

class App extends Component {
  /**
   * 指定config的类型声明为: Taro.Config
   *
   * 由于 typescript 对于 object 类型推导只能推出 Key 的基本类型
   * 对于像 navigationBarTextStyle: 'black' 这样的推导出的类型是 string
   * 提示和声明 navigationBarTextStyle: 'black' | 'white' 类型冲突, 需要显示声明类型
   */
  static options = {
    addGlobalClass: true
  }
 ....
}

每个组件都继承了这个组件

export default class BeastComponent<P = {}, S = {}> extends Component<P, S> {
  static options = {
    addGlobalClass: true
  }
}

在页面上组件外部样式无法覆盖内部的样式

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants