Javascript

浏览模式: 标准 | 列表 | 图文
vue + sortable js的一些简单操作

vue + sortable js的一些简单操作

用sortable处理排序,代码真的很少,不需要引用什么vue-sortable之类的。但会有个问题。。实际的DOM和Vnode可能会显示不一致。往往拖完了,序号变了,数据变了(可以通过console.log来查看),但就是DOM没变。

后来是搜索到这篇文章,写的比较详情:https://www.jianshu.com/p/d92b9efe3e6a,我就不多写了。我的几行代码类似如下:

dragable() {
let contentRow = document.querySelector('.content-row');//指定element,如果有ID最好,在这个下面的元素,最好都是统一类型的,如果当前元素是ul,那就是拖li
Sortable.create(contentRow, {
onEnd: ({newIndex, oldIndex}) => { //监听拖动结束事件
let newline = contentRow.children[newIndex],
oldline = contentRow.children[oldIndex];
// 先删除移动的节点 ,再插入移动的节点到原有节点,还原了移动的操作
//这里是操作DOM
contentRow.removeChild(newline);
contentRow.insertBefore(newline, newIndex > oldIndex? oldline : oldline.nextSibling);
//这里是操作数据
let currRow = this.items.splice(oldIndex, 1)[0];
this.items.splice(newIndex, 0, currRow);

}
});
},

这个方法是vue的methods中的。不复杂。看看就行。记录一下。原来我觉得很简单。这个方法的最后两行就行了。。。但就是DOM不变。参考了上面链接中的内容才加了那两行。

wechaty 在 分拆的文件中使用自身变量

wechaty 在 分拆的文件中使用自身变量

wechaty是目前看来最优雅的机器人库了。使用方式也很简单,官方的demo例子就几行代码,不过看到他们的hot-import-bot.js就感觉慌了。毕竟在同一个页面里,还能直接使用bot变量(例子中实例化的时候,用的是bot,const bot = new Wechaty());但分拆成单独的文件后就没有相应的用法。

看源码中是有这样的代码:

public on (event: 'dong', listener: string | ((this: Wechaty, data?: string) => void)) : this

是看到确实对每一个件事都是传入两个参数,然而demo里都是类似这样:

async function onLogin (user) {
console.log(`${user} login`)
}

也没有看到有传入第二个变量。如果你主动 传入第二个参数,会直接报undefined。在这样分拆的页面怎么使用bot呢?

我找了一下node的全局变量,发现nodejs有一个global变量。嗯,变量名就叫:global.....,于是将除了message以外的事件放到主文件index.js中。并在onlogin中赋值:global.bot = bot;

然后在分拆后的message中,const bot = global.bot; 果然就可以用了。

问了一下群里,发现有人也这样用,但。。。Huan Li(Huan (李卓桓))说了,其实可以在方法里面直接 const wechaty = this;就可以用了。。。

这么说。。。this就可以直接用?果然不会Ts就是不懂。然后 他还更新了一下 git,并在里面加入了使用方法,详见:https://github.com/wechaty/wechaty-getting-started/blob/master/examples/professional/hot-import-bot/listeners/on-friend.js

/**
* Wechaty - https://github.com/chatie/wechaty
*
* @copyright 2016-2018 Huan LI <zixia@zixia.net>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
async function onFriend (contact, request) {
/**
* We can get the Wechaty bot instance from this:
* `const wechaty = this`
* Or use `this` directly:
* `console.info(this.userSelf())`
*/
if(request){
let name = contact.name()
// await request.accept()

console.log(`Contact: ${name} send request ${request.hello()}`)
}
}

module.exports = onFriend
看到 if(request) 上的注释了么。。。就是这样简单。

最后想说的是,感觉

public on (event: 'dong', listener: string | ((this: Wechaty, data?: string) => void)) : this

这个this的用法就象python的self一样。虽然不懂,但是感觉很厉害的样子。

最近在尝试使用他做了个小机器人,就是当群里有人问天气的时候自动回复了一下。。。目前只是demo。没有细纠,但这时候发现 Room.alias(Contact)。没有拿到群昵称,准备这两天再看看问题是啥。

yarn:error Incorrect integrity when fetching from the cache

yarn:error Incorrect integrity when fetching from the cache

yarn add laravel-echo-server

yarn add v1.19.0
[1/4] ? Resolving packages...
[2/4] ? Fetching packages...
error Incorrect integrity when fetching from the cache
info Visit https://yarnpkg.com/en/docs/cli/add for documentation about this command.
解决方法:
1、yarn config set unsafe-disable-integrity-migration false
2、yarn cache clean
3、yarn install -f 或者 直接 yarn add ..
问题解决。
setWechatTitle 触发过早导致ios input无法输入

setWechatTitle 触发过早导致ios input无法输入

setWechatTitle 指的是这个插件:https://juejin.im/post/5cff17f45188252dd239943a

在微信里,涉及到后续改title总会有不能刷新,或者不能显示动态title的问题。这个插件就是为了解决它的。
然而,今天遇到的问题是。过早的设置this.wechat.title,导致input框无法输入(用的是element-ui)。测试过。普通 的input框可以(估计两者在CSS上会有冲突,没有细究)
最后简单偷懒的方法是将 这个触发放到了wx.ready(里)。至少表明,我页面元素都加载完了,才触发的。此时不再有任何冲突。
没有时间查问题出在哪里,是哪个语句导致的(因为只有IOS上有这个问题,android没有。而且,IOS上的 -webkit-user-select:auto 这个CSS也设置了,所有的都是无效)

element-UI table如何按比例设置宽度

做后台,还真是首推ElementUI,毕竟规范,虽然东西比其他的少了一点,比如没有默认的ul/li效果之类的,但这些标准元素你可以引用一些第三方库,比如bootstrap,只要用他的CSS就行了。

当然,elementui也会有小问题,比如这次新版里推出的PopConfirm,好看是好看,但@confim/@cancel都没有(看官方的issue,有人提出是@onConfirm/@onCancel,但都没有用,也有人说是@on-confirm。。。。没有一一试,先用popup解决了)

这一篇说的是element-ui的Table组件,每个column都可以设置width,但这个width,因为没有单位,说不准是px?还是rem,如果一个表格的列不多,你这样设置的话,在大屏和小屏上面就很尴尬,要么全部偏左,要么撑破页面。因为他默认居然没有百分比设置(应该说width不支持百分比),但确实有人发现是可以支持,原来,el-table-column还有一个 min-width,这个就支持百分比,你可以设置<el-table-column min-width="15%"></el-table-column>,记得总数不要超过100%,否则还是会撑出或者会不起作用(总感觉有点妖)

laravel mix extract之后.scss编译出来是空文件

laravel mix extract之后.scss编译出来是空文件

应该不算是我姿势不对吧,网上不少人有这个问题:https://github.com/JeffreyWay/laravel-mix/issues/1914。

其实代码非常简单,如果是这样:
mix
.js('resources/front/visitors/visitors.js', 'public/js')
.sass('resources/front/visitors/sass/visitors.scss', 'public/css')
那就一切正常。如果加了这么一行:
mix
.js('resources/front/visitors/visitors.js', 'public/js')
.sass('resources/front/visitors/sass/visitors.scss', 'public/css')
.extract([....])
则生成出来的visitors.css为空文件。生成空文件的时候,还认为自己是从几个vue文件里做了提取。但事实上scss这个文件是孤立的。不含在vue里(确实是有一个提取vuestyle的参数,但我没设置),laravel-mix的作者居然说:No, answer is don't upgrade if you're using dynamic imports. See the Mix 4 release notes.
评论里有人说实在不行就分成两个js文件来处理吧。嗯,事实上我也这样做了,比如我我因为前后台分开,就这么折腾的,但也会带来问题,比如,后一个生成的manifest.json会覆盖前面的(如果你不用{{mix()}}函数,那不会遇到这个问题,因为你不用它。。。但如果你用了,就会遇到各种不同的小问题,除非你放在一个mix.js文件里,只是用路由来解决)
最后我确实是分拆了,利用了concurrently,直接加了一个scripts:
{
"start":"concurrently \"npm:watch*\""
}
执行npm run start,就OK了。如果你不想看完整的输出。上面的scripts中可以改成:
{
"start":"concurrently -r \"npm:watch*\""
}
是的,你没有看错,就加了一个-r的参数
weui小程序安装出错:[WARNING] 找不到编译器:wepy-compiler-less。

weui小程序安装出错:[WARNING] 找不到编译器:wepy-compiler-less。

直接git clone weui小程序的项目,然后wepy build会出错。即使之前你运行npm install。

我是直接报:[WARNING] 找不到编译器:wepy-compiler-less。
解决方案不复杂:先npm install less 再 wepy build,一般就解决了。build的时候会自己安装wepy-compiler-less。
如果象我,还遇到下面的错误
npm WARN eslint-config-standard@7.1.0 requires a peer of eslint-plugin-promise@>=3.3.0 but none is installed. You must install peer dependencies yourself.
npm WARN wepy-compiler-less@1.3.14 requires a peer of less@^3.8.1 but none is installed. You must install peer dependencies yourself.
npm WARN wepy-weui-demo@1.0.0 No repository field.
+ wepy-compiler-less@1.3.14
updated 1 package and audited 7975 packages in 3.378s
found 10 vulnerabilities (7 low, 3 moderate)
这时候就需要根据提示:npm install -g eslint-plugin-promise@>=3.3.0,进行安装(其实上面也有说,是因为less没有安装)
这样的话,其实问题就都好解决
aliyun:One or more of the sp....

aliyun:One or more of the sp....

Aliyun oss 报错: One or more of the specified parts could not be found or the specified entity tag might not have matched the part's entity tag.

报错是在multipartUpload方法中。如果要解决也很方便,在跨域设置中,暴露的headers中增加一个:ETag,即可,就是这么简单,但,真心花了不少时间处理(最后要看这里:https://help.aliyun.com/document_detail/32069.htm,如果用了分片,还是要再加一个:etag和x-oss-request-id)
有点小意外的是。AliOss的文档中有写:oss.put(name, object ),说是第二个参数支持File和Blob对象(嗯,在浏览器中)
file {String|Buffer|ReadStream|File(only support Browser)|Blob(only support Browser)} object local path, content buffer or ReadStream content instance use in Node, Blob and html5 File
然而在实际使用的时候,却是报错:TypeError: Must provide String/Buffer/ReadableStream for put.,这,简直是忽悠人啊。再比如说,multipartUpload,他说:
file {String|File(only support Browser)|Blob(only support Browser)} file path or HTML5 Web File or web Blob
这回就真实是支持的。
----
难道区别就在于上面写的是:object local path而下面写的是 file path??
不管了。已解决。就是有点渗的慌
browser.min.js 报错:Cannot read property 'keys' of undefined

browser.min.js 报错:Cannot read property 'keys' of undefined

在IE下,果然什么样的问题都会发生,为了兼容旧版浏览器,大家都知道要加载browser.min.js。然而。。。问题还是来了,就象标题那样。

查了一些资料,发现很多人都遇到过,原来。。。。居然是browser.min.js的版本太高了,降为 5.8.24 ,世界清静了
然而还是遇到一点问题,为了想用一些新的语法,用了<script type="text/babel">,但这样却导致vue 中的变量无法识别。尴尬 ,这一点还没有想到好办法
没办法用纯VUE进行开发,只能先PHP+VUE进行混写了
非专业前端做页面要注意的几个事项

非专业前端做页面要注意的几个事项

为了该死的IE,可能会需要这样

1、如果你的代码里用到了ES6的语法,比如:const a = 1;或者 a(){}等,记得 加载 browser.min.js ,如果还要支持 promise,那就加载browser-polyfill.min.js,下载地址:https://cdn.jsdelivr.net/npm/browser-polyfill@3.20.2/index.min.js,或者先npm install babel-core@7,然后复制其中的browser.min.js到你的项目里。如果文件中不含polyfill,还得 单独去下载一下
2、如果页面中引用了一些第三方的jQuery组件,记得加载1.x的版本。因为 从2开始不支持IE8了。如果IE9以下,那是无所谓
有些东西,还是需要记录一下。都是坑啊。。。然而直到IE11,都对ES6支持不好。所以。browser.js还是跑不 掉